version 0.3.0, added disable/enable comics
[comic-widget] / src / usr / lib / hildon-desktop / comicwidget.py
index 72c4738..4b6239d 100644 (file)
@@ -16,14 +16,16 @@ supports_alpha = False
 
 # constants. dbfile is the location of the csv
 # comiccache is the location of the images
-APP_VERSION = "0.2.5"
+APP_VERSION = "0.3.0"
 
 basedbdir = "/opt/comic-widget/db/"
 imagedir = "/opt/comic-widget/images/"
 dbdir = "/home/user/.comic-widget/"
+activecomics = dbdir + "activecomics.cfg"
 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"},
+               "phd":{"name":"PHD Comics","link":"http://www.phdcomics.com/","start":1240,"dbfile":dbdir + "comicdb.phd.csv"},
                "dilbert":{"name":"Dilbert","link":"http://dilbert.com.com/","start":"2009-01-01","dbfile":dbdir + "comicdb.dilbert.csv"}}
 
 # handling of the comics
@@ -43,7 +45,7 @@ class ComicDb():
                for row in dbr:
                        self.db.insert(0,row)
                dbf.close()
-               self.currentcomic = 0           
+               self.currentcomic = 0
 
 
 
@@ -57,10 +59,10 @@ class ComicDb():
                # if not, copy it.
                else:
                        print "file " + self.dbfile + " not found"
-                       if not os.path.exists(dbdir + self.comic + "/"):
-                               print "path not found: " + dbdir + self.comic + "/"
+                       if not os.path.exists(dbdir):
+                               print "path not found: " + dbdir
                                try:
-                                       os.makedirs(dbdir + self.comic + "/")
+                                       os.makedirs(dbdir)
                                except:
                                        print "comic db creation failed on mkdir"
                                        exit()
@@ -68,13 +70,15 @@ class ComicDb():
                                shutil.copyfile(basedbdir + "comicdb." + self.comic + ".csv", dbdir + "comicdb." + self.comic + ".csv")
                        except:
                                print "comic db creation failed on copy"
-                               exit()
+                               print "creating new..."
+                               dbf = open(self.dbfile, 'w')
+                               dbf.write('comic,id,link,url,filename,title\n')
+                               dbf.close()
                        if os.path.isfile(self.dbfile) == True:
                                dbf = open(self.dbfile, 'r')
                                return dbf
                        else:
                                print "comic db creation failed after copy"
-                               exit()
 
 
        def get_comic(self):
@@ -178,7 +182,7 @@ class ComicDb():
        def fetch_latest_std(self, comic, latest):
                print "fetching new after " + str(comic) + " " + str(latest)
                dateid = False
-               if len(latest) == 10:
+               if len(str(latest)) == 10:
                        # date id.
                        dateid = True
                        dt = string.split(latest, "-")
@@ -188,11 +192,15 @@ class ComicDb():
                else:
                        comicid = int(latest) + 1
 
-
+               lasturl = self.db[0]['url']
                while True:
                        irow = self.get_irow(comic, comicid)
                        if irow:
                                print "got irow: " + str(irow)
+                               if irow[3] == lasturl:
+                                       print "Looping the same, break break break!"
+                                       break
+                               lasturl = irow[3]
                                print "inserting..."
                                self.insert_row(irow)
                                if dateid:
@@ -220,6 +228,8 @@ class ComicDb():
                        link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
                elif comic == 'dilbert':
                        link = "http://dilbert.com/" + str(number) + "/"
+               elif comic == 'phd':
+                       link = "http://www.phdcomics.com/comics/archive.php?comicid=" + str(number)
                else:
                        return False
 
@@ -273,6 +283,21 @@ class ComicDb():
                                        return False
                                title = ""
 
+                       elif comic == 'phd':
+                               s = f.read()
+                               f.close()
+                               # title:
+                               splt = string.split(s, '<title>PHD Comics: ', 1)
+                               splt = string.split(splt[1], '</title>', 1)
+                               title = splt[0];
+                               splt = string.split(splt[1], '<td bgcolor=#FFFFFF align=center><img src=', 1)
+                               splt = string.split(splt[1], ' align=top>', 1)
+                               url = splt[0]
+                               if len(url) < 49:
+                                       print "Fake 404! Break break break!"
+                                       return False
+                               
+
                        splt2 = string.rsplit(url, "/", 1)
                        filename = splt2[1]
                        irow = [comic,number,link,url,filename,title]
@@ -287,13 +312,15 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                hildondesktop.HomePluginItem.__init__(self)
                global supports_alpha
                self.set_settings(True)
+               self.active_comics = self.get_active_comics()
                self.connect("show-settings", self.show_options)
-               self.osso_c = osso.Context("comic-widget", "0.0.1", False)
+               self.osso_c = osso.Context("comic-widget", "0.3.0", False)
                self.osso_rpc = osso.Rpc(self.osso_c)
-               self.keys = comics.keys()
-               self.keypointer = 1
-               self.db = ComicDb(self.keys[self.keypointer])
-               self.comicname = comics[self.keys[self.keypointer]]['name']
+               self.keypointer = 0
+               if len(self.active_comics) < 1:
+                       self.active_comics = ['xkcd']
+               self.db = ComicDb(self.active_comics[self.keypointer])
+               self.comicname = comics[self.active_comics[self.keypointer]]['name']
                self.set_name = "comicwidget"
                screen = self.get_screen()
                colormap = screen.get_rgba_colormap()
@@ -516,12 +543,16 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                                self.e_goweb.show_all()
 
                        elif func == 'switch':
-                               self.keypointer = (self.keypointer + 1) % 3
+                               self.active_comics = self.get_active_comics()
+                               print "active comics: " + str(self.active_comics)
+                               self.keypointer = (self.keypointer + 1) % len(self.active_comics)
                                self.imgvpos = 0
                                self.db = []
-                               print "switching to " + self.keys[self.keypointer]
-                               self.comicname = comics[self.keys[self.keypointer]]['name']
-                               self.db = ComicDb(self.keys[self.keypointer])
+                               print "switching to " + self.active_comics[self.keypointer]
+                               self.comicname = comics[self.active_comics[self.keypointer]]['name']
+                               self.db = ComicDb(self.active_comics[self.keypointer])
+                               self.e_open.remove(self.comic_image)
+                               self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
                                self.e_goweb.remove(self.label)
                                titl = str(self.db.db[self.db.currentcomic]['title'])
                                if len(titl) > 22:
@@ -529,8 +560,6 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                                self.label.set_markup('<span size="12000" face="monospace"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '</span>')
                                self.e_goweb.add(self.label)
                                self.e_goweb.show_all()
-                               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()  
 
@@ -611,7 +640,52 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
 
                return False
 
-# ************************* OPTIONS ********************************
+
+       def get_active_comics(self):
+               print "get list"
+
+               if os.path.isfile(activecomics) == True:
+                       print "found " + activecomics
+                       dbf = open(activecomics, 'r')
+                       dbr = csv.reader(dbf)
+                       try:
+                               active = dbr.next()
+                       except:
+                               active = []
+                       dbf.close()
+                       return active
+               # if not, copy it.
+               else:
+                       print "file " + activecomics + " not found"
+                       if not os.path.exists(dbdir):
+                               print "path not found: " + dbdir
+                               try:
+                                       os.makedirs(dbdir)
+                               except:
+                                       print "settings dir creation failed. Exiting."
+                                       exit()
+                       try:
+#                              dbf = file(activecomics, 'w')
+#                              dbf.close()
+                               dbf = open(activecomics, 'w')
+                       except:
+                               print "error storing settings"
+                               exit()
+                       dbw = csv.writer(dbf)
+                       dbw.writerow(comics.keys())
+                       print comics.keys()
+                       dbf.close()
+                       ret = comics.keys()
+                       return ret
+
+               
+               
+               
+               #check if settings file exists
+
+               #if not, write defaults
+
+# ************************* OPTION DIALOGS ********************************
 
        def show_options(self, widget):
                dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
@@ -621,7 +695,13 @@ class ComicHomePlugin(hildondesktop.HomePluginItem):
                about_button.set_alignment(0,0,0,0)
                about_button.connect("clicked", self.show_about)
 
+               comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+               comics_button.set_text("Comics", "Choose which comics to view")
+               comics_button.set_alignment(0,0,0,0)
+               comics_button.connect("clicked", self.show_comics)
+
                dialog.vbox.pack_start(about_button, True, True, 0)
+               dialog.vbox.pack_start(comics_button, True, True, 0)
 
                dialog.show_all()
                dialog.run()
@@ -633,15 +713,74 @@ 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>\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_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_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/>.""")
                dialog.set_wrap_license(True)
                dialog.show_all()
                dialog.run()
                dialog.destroy()
 
+       def show_comics(self, widget):
+               dialog = gtk.Dialog("Configure Search Engines", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
+               comiclist = comics.keys()
+               comiclist.sort()
+               buttonlist = {}
+               for comicid in comiclist:
+                       buttonlist[comicid] = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
+                       print buttonlist[comicid].get_name()
+                       
+                       print buttonlist[comicid].set_alignment(0, 0.5, 0, 0.5) 
+                       active = False
+                       for acomic in self.active_comics:
+                               if acomic == comicid:
+                                       active = True
+                                       break
+                       if active:
+                               buttonlist[comicid].set_text(comics[comicid]['name'] + " *", comicid + ": click to remove")
+                               buttonlist[comicid].connect("clicked", self.remove_comic)
+                       else:
+                               buttonlist[comicid].set_text(comics[comicid]['name'], comicid + ": click to add")
+                               buttonlist[comicid].connect("clicked", self.add_comic)
+                       dialog.vbox.pack_start(buttonlist[comicid], True, True, 0)
+
+               
+               dialog.show_all()
+               dialog.run()
+               dialog.destroy()
+                               
+       def add_comic(self, widget):
+               print "add " + widget.get_title()
+               splt = string.split(widget.get_value(), ':', 1)
+               comicid = splt[0]
+               if os.path.isfile(activecomics) == True:
+                       print "added " + comicid + " to " + str(self.active_comics)
+                       try:
+                               found = self.active_comics.index(comicid)
+                       except:
+                               self.active_comics.append(comicid)
+                       dbf = open(activecomics, 'w')
+                       dbw = csv.writer(dbf)
+                       dbw.writerow(self.active_comics)
+                       dbf.close()
+               widget.set_text(comics[comicid]['name'] + " *", comicid + ": click to remove")
+
+       def remove_comic(self, widget):
+               print "remove " + widget.get_title()
+               if len(self.active_comics) == 1:
+                       return
+               splt = string.split(widget.get_value(), ':', 1)
+               comicid = splt[0]
+               if os.path.isfile(activecomics) == True:
+                       print "removing " + comicid + " from " + str(self.active_comics)
+                       del self.active_comics[self.active_comics.index(comicid)]
+                       dbf = open(activecomics, 'w')
+                       dbw = csv.writer(dbf)
+                       dbw.writerow(self.active_comics)
+                       dbf.close()
+               widget.set_text(comics[comicid]['name'], comicid + ": click to add")
+               
 hd_plugin_type = ComicHomePlugin