added Lari Tuononens scripts
[comic-widget] / src / usr / lib / hildon-desktop / comicwidget.py
index 3d3f6af..303b620 100644 (file)
@@ -18,13 +18,13 @@ socket.setdefaulttimeout(30)
 supports_alpha = False
 
 # LOGGING!
-# sys.stdout = open('/home/user/.comic-widget/output_log.txt', 'a')
-# sys.stderr = open('/home/user/.comic-widget/error_log.txt', 'a')
+# sys.stdout = open('/home/user/.comic-widget/output_log.txt', 'a', 0)
+# sys.stderr = open('/home/user/.comic-widget/error_log.txt', 'a', 0)
 # print "Start logging!"
 
 # constants. dbfile is the location of the csv
 # comiccache is the location of the images
-APP_VERSION = "0.4.2"
+APP_VERSION = "0.4.4"
 
 basedbdir = "/opt/comic-widget/db/"
 imagedir = "/opt/comic-widget/images/"
@@ -138,7 +138,6 @@ comics = {
                "wulff":{"name":"Wulffmorgenthaler","link":"http://wulffmorgenthaler.com/","start":"edd3411b-96ca-4d93-bd5f-0cf1deb67c8a","dbfile":dbdir + "comicdb.wulff.csv"},
                "xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":dbdir + "comicdb.xkcd.csv"},
                "zack_hillcomicscom":{"name":"Zack Hill","link":'http://comics.com/zack_hill/',"start":"2010-02-01","dbfile":dbdir + "comicdb.zackhill.csv"},
-
                "amazing_spidermanckdm":{"name":"The Amazing Spider-Man","link":'http://content.comicskingdom.net/Spiderman/',"start":"20100209","dbfile":dbdir + "comicdb.spiderman.csv"},
                "apartment_3-gckdm":{"name":"Apartment 3-G","link":'http://content.comicskingdom.net/Apartment_3-G/',"start":"20100209","dbfile":dbdir + "comicdb.partment3g.csv"},
                "arcticckdm":{"name":"Arctic Circle","link":'http://content.comicskingdom.net/Arctic/',"start":"20100209","dbfile":dbdir + "comicdb.arctic.csv"},
@@ -202,6 +201,8 @@ comics = {
                "toddckdm":{"name":"Todd the Dinosaur","link":'http://content.comicskingdom.net/Todd/',"start":"20100209","dbfile":dbdir + "comicdb.todd.csv"},
                "zippy_the_pinheadckdm":{"name":"Zippy the Pinhead","link":'http://content.comicskingdom.net/Zippy_the_Pinhead/',"start":"20100209","dbfile":dbdir + "comicdb.zippythepinhead.csv"},
                "zitsckdm":{"name":"Zits","link":'http://content.comicskingdom.net/Zits/',"start":"20100209","dbfile":dbdir + "comicdb.zits.csv"},
+               "calvinandhobbesmatinmaapallo":{"name":"Calvin and Hobbes","link":"http://matin.maapallo.org/site.pl/selain/?c=lassijaleevi&i=","start":"2008","dbfile":dbdir + "comicdb.calvinandhobbes.csv"},\r
+               "viivijawagnermatinmaapallo":{"name":"Viivi ja Wagner","link":"http://matin.maapallo.org/site.pl/selain/?c=viivijawagner&i=","start":"2600","dbfile":dbdir + "comicdb.viivijawagner.csv"},\r
 
 
 
@@ -269,6 +270,30 @@ class ComicDb():
                                print "comic db creation failed after copy"
 
 
+       def get_stored(self):
+               try:
+                       idf = open(self.dbfile + ".last", 'r')
+                       retval = idf.read()
+                       print "got stored index :" + str(retval)
+                       idf.close()
+                       return int(retval)
+               except:
+                       return 0
+
+
+       def store_last(self, sid = 0):
+               try:
+                       idf = open(self.dbfile + ".last", 'w')
+                       if sid > 0:
+                               idf.write(str(sid))
+                       else:
+                               idf.write(str(self.currentcomic))
+                       idf.close()
+                       return True
+               except:                 
+                       return False
+
+
        def get_comic(self):
                # print str(self.currentcomic)
                if self.currentcomic < 0:
@@ -343,6 +368,9 @@ class ComicDb():
                dbw = csv.writer(dbf)
                dbw.writerow(irow)
                dbf.close()
+               stored = self.get_stored()
+               if stored > 0:
+                       self.store_last(stored + 1)
 
        def insert_row_first(self, irow):
                dbf = open(self.dbfile, 'w')
@@ -385,7 +413,7 @@ class ComicDb():
 # fetch earlier
        def fetch_earlier(self, comic, earliest):
                print "fetch before, " + comic + " earliest"
-               if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
+               if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom" or comic[-13:] == "matinmaapallo":
                        print "getting get_prev_id..."
                        comicid = self.get_prev_id(comic,earliest)
                        if not comicid:
@@ -419,14 +447,14 @@ class ComicDb():
                        print "\ninserting...\n"
                        self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
                else:
-                       print "No comic found at " + comicid
+                       print "No comic found at " + str(comicid)
 
 
 
 
 
        def fetch_newer(self, comic, newest):
-               if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
+               if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom" or comic[-13:] == "matinmaapallo":
                        comicid = self.get_next_id(comic,newest)
                        if not comicid:
                                print "already at last comic"
@@ -619,7 +647,35 @@ class ComicDb():
                                s = f.read()
                                f.close()
                                return self.parse_comics_com(comic, s, 'next')
-
+               elif comic[-13:] == "matinmaapallo":\r
+                       realcomic = string.split( comic, 'matinmaapallo', 1)\r
+                       linkname = realcomic[0]\r
+                       link = "http://matin.maapallo.org/site.pl/selain/?c=" + linkname + "&i=" + str(number)\r
+                       print "link: " + link\r
+                       try:\r
+                               f = urllib2.urlopen(link)\r
+                               hcode = f.code\r
+                       except:\r
+                               hcode = 404\r
+                       print "got hcode = " + str(hcode) + "\n"\r
+                       print "I am now here"\r
+                       if (hcode != 200):\r
+                               return False\r
+                       else:\r
+                               print "checking next matin maapallo issue.."\r
+                               s = f.read()\r
+                               f.close()\r
+                               # title:\r
+                               splt = string.split(s, 'seuraava', 1)\r
+                               if len(splt) < 2:\r
+                                       print "no 'prev' found"\r
+                                       return False\r
+                               else:\r
+                                       next = str( int(number) + 1 )\r
+                                       print "got next: " + next\r
+                                       return next\r
+               else:
+                       return False
 
 
 
@@ -718,8 +774,18 @@ class ComicDb():
                                s = f.read()
                                f.close()
                                return self.parse_comics_com(comic, s, 'prev')
-
-
+               elif comic[-13:] == 'matinmaapallo':\r
+                       print "checking matin maapallo..."\r
+                       prevint = int( number ) - 1\r
+                       if ( prevint  == 0 ):\r
+                               print "cannot go previous"\r
+                               return False\r
+                       else:\r
+                               print "got number: " + number\r
+                               print "got previous: " + str(prevint)\r
+                               return str(prevint)
+               else:
+                       return False
 
 
        def get_irow(self, comic, number):
@@ -746,6 +812,8 @@ class ComicDb():
                        link = "http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=" + str(number)
                elif comic[-9:] == 'comicscom': 
                        link = comics[comic]['link'] + str(number) + "/"
+               elif comic[-13:] == 'matinmaapallo':    
+                       link = comics[comic]['link'] + str(number)
                elif comic[-4:] == 'ckdm':
                        splt = string.rsplit(comics[comic]['link'], "/", 2)
                        cname = splt[1]
@@ -918,6 +986,23 @@ class ComicDb():
                                url = self.parse_comics_com(comic, f.read(), 'url') 
                                f.close()
                                title = number
+                       elif comic[-13:] == 'matinmaapallo':
+                               # matin maapallo links is type .gif
+                               s = f.read()
+                               f.close()
+                               splt = string.split(s, 'http://matin.maapallo.org/kuvatuuppari/', 1)
+                               #print "splitted"  + "\n"
+                               flnm = string.split(splt[1], ".gif", 1)
+                               #print flnm[0]  + "\n" + "url: "
+                               url = 'http://matin.maapallo.org/kuvatuuppari/' + flnm[0] + '.gif'
+                               #print url
+                               splt3 = string.split(s, '<h4>', 1)
+                               splt4 = string.split(splt3[1], '</h4>', 1)
+                               title = splt4[0]
+                               print title
+                               filename = str(number) + ".gif"
+                               irow = [comic,number,link,url,filename,title]
+                               return irow
 
                        elif comic[-4:] == 'ckdm':
                                url = link
@@ -1057,9 +1142,11 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
 
                self.e_goweb.add(self.label)
                self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
-               self.e_goweb.connect("button-release-event", self.view_comic)
+               self.e_goweb.connect("button-release-event", self.view_comic_web)
 
                self.e_open.add(self.comic_image)
+               self.e_open.set_events(gtk.gdk.BUTTON_PRESS_MASK)
+               self.e_open.connect("button-press-event", self.view_comic)
 
                # self.e_switch.add(self.switch)
                self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
@@ -1109,7 +1196,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
 
        # **************** Cairo draw functions ***************
 
-       def realize(self, widget):
+       def do_realize(self):
                global supports_alpha
 
                # To check if the display supports alpha channels, get the colormap
@@ -1126,7 +1213,6 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                # Now we have a colormap appropriate for the screen, use it
                self.set_colormap(colormap)
                hildondesktop.HomePluginItem.do_realize(self)
-               return False
 
        def do_expose_event(widget, event):
                cr = widget.window.cairo_create()
@@ -1146,7 +1232,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                        cr.set_source_rgb (0.0, 0.0, 0.0)
                        
                cr.fill_preserve()
-               return False
+               hildondesktop.HomePluginItem.do_expose_event(widget,event)
 
        # Function that draws a button preloaded in the images dictionary.
        def draw(self, target, postfix):
@@ -1213,8 +1299,16 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                                self.e_goweb.show_all()
 
                        elif func == 'switch':
+                               # Check if store needed..
+                               if not self.db.currentcomic == 0:
+                                       print "Storing location " + str(self.db.currentcomic)
+                                       if self.db.store_last():
+                                               print "stored successfully"
+                                       else:
+                                               print "store failed"
+                                
                                self.active_comics = self.get_active_comics()
-                               print "closing log"
+                               # print "closing log"
                                #sys.stdout.close()
                                #sys.stdout = sys.__stdout__
                                #sys.stderr.close()
@@ -1255,12 +1349,20 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                self.draw(widget, "0")
 
        # no fancy cairo stuff here. simply launches url in browser.
-       def view_comic(self, widget, event):
+       def view_comic_web(self, widget, event):
                if not event.type == gtk.gdk.BUTTON_RELEASE:
                        return False
                link = self.db.get_link()
                self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
 
+       # simply launches image in browser.
+       def view_comic(self, widget, event):
+               if not event.type == gtk.gdk._2BUTTON_PRESS:
+                       return False
+               #link = self.db.get_link()
+               link = 'file:///home/user/MyDocs/.comics/' + self.db.comic + '/' + self.db.db[self.db.currentcomic]['filename']
+               self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
+
 
 
        # this function modifies the image to fit the widget and then returns it.
@@ -1272,11 +1374,22 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                height = int(self.sizes['height'])
                picwidth = width - 2
                maxheight = height - 48
-
+               retimg = gtk.Image()
+               if os.path.isfile(filename) == False:
+                       filename = "/opt/comic-widget/images/up0-48x48.png"
+                       pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
+                       parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
+                       parent_buf.fill(0x00000000)
+                       tmpy = (maxheight / 2) - 24
+                       tmpx = (width / 2) - 24
+                       pixbuf.copy_area(0,0,48,48,parent_buf,int(tmpx),int(tmpy))
+                       retimg.set_from_pixbuf(parent_buf)
+                       print retimg.get_pixel_size()
+                       print "No image found, posting up arrow"
+                       return retimg
                pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
                parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
                parent_buf.fill(0xffffffff)
-               retimg = gtk.Image()
                # get correct width/height
                new_height = 1
                new_width = picwidth
@@ -1284,12 +1397,14 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
                print "scaled height: " + str(scaled_buf.get_height())
                if scaled_buf.get_height() > maxheight:
+                       self.scrollable = True
                        if scaled_buf.get_height() - (vpos + maxheight) < 0:
                                vpos = scaled_buf.get_height() - maxheight
                        scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
                        # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
                        self.imgvpos = vpos
                else:
+                       self.scrollable = False
                        tmpy = maxheight - scaled_buf.get_height()
                        if tmpy > 0:
                                tmpy = tmpy / 2
@@ -1371,6 +1486,20 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
 
                #if not, write defaults
 
+       def goto_comic(self, index):
+               self.db.currentcomic = index
+               self.e_open.remove(self.comic_image)
+               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()
+               self.e_goweb.remove(self.label)
+               titl = str(self.db.db[self.db.currentcomic]['title'])
+               if len(titl) > 22:
+                       titl = titl[:19] + "..."
+               self.label.set_markup(self.get_markup())
+               self.e_goweb.add(self.label)
+               self.e_goweb.show_all()
+       
 # ************************* OPTION DIALOGS ********************************
 
        def show_options(self, widget):
@@ -1387,12 +1516,32 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                comics_button.set_alignment(0,0,0,0)
                comics_button.connect("clicked", self.show_comics)
 
+               navfirst_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+               navfirst_button.set_text("Go to earliest", "Jump to the oldest cached comic")
+               navfirst_button.set_alignment(0,0,0,0)
+               navfirst_button.connect("clicked", self.show_nav, "oldest")
+
+               navlast_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+               navlast_button.set_text("Go to latest", "Jump to the newest cached comic")
+               navlast_button.set_alignment(0,0,0,0)
+               navlast_button.connect("clicked", self.show_nav, "newest")
+
+               navstored_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+               navstored_button.set_text("Go to stored comic", "Jump to where you were before you switched comics")
+               navstored_button.set_alignment(0,0,0,0)
+               navstored_button.connect("clicked", self.show_nav, "stored")
+
                # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
                # size_button.set_text("Change size", "Change the size of the widget")
                # size_button.set_alignment(0,0,0,0)
                # size_button.connect("clicked", self.show_size)
 
+
+
                print "adding buttons to dialog, starting with comics"
+               dialog.vbox.pack_start(navstored_button, True, True, 0)
+               dialog.vbox.pack_start(navfirst_button, True, True, 0)
+               dialog.vbox.pack_start(navlast_button, True, True, 0)
                dialog.vbox.pack_start(comics_button, True, True, 0)
                print "adding about button"
                dialog.vbox.pack_start(about_button, True, True, 0)
@@ -1402,6 +1551,14 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                dialog.run()
                dialog.destroy()
 
+       def show_nav(self, widget, goto):
+               if goto == "stored":
+                       print "look for stored file"
+                       self.goto_comic(self.db.get_stored())
+               elif goto == "newest":
+                       self.goto_comic(0)
+               elif goto == "oldest":
+                       self.goto_comic(len(self.db.db) - 1)
        def show_about(self, widget):
                print "in about dialog"
                dialog = gtk.AboutDialog()
@@ -1409,7 +1566,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                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>\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."])
+               dialog.set_authors(["Marcus Wikstrom <mece@ovi.com>\nAdditional code by Lari Tuononen\nLogo by Martin Wikstrom\n\nSpecial thanks to all the great python developers on t.m.o,\n and the helpful wizards on #maemo."])
                dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
                dialog.set_comments("Silly rabbit, tricks are for kids.")
                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/>.""")