', 1) + splt5 = string.split(splt4[1], '
', 1) + title = splt5[0] + #print title + filename = str(number) + ".gif" + irow = [comic,number,link,url,filename,title] + return irow + elif comic[-4:] == 'ckdm': + url = link + title = number[:4] + "-" + number[4:6] + "-" + number[6:8] splt2 = string.rsplit(url, "/", 1) filename = splt2[1] + try: + if filename == self.db[0]['filename']: + print "already exists! Break break break!" + return False + except: + pass irow = [comic,number,link,url,filename,title] return irow + def parse_comics_com(self, comicname, source, get_what): + # rel="{StripID:309486, ComicID:36, Type:'Comic', DateStrip:'2010-02-06', URL_Comic: 'girls&sports', Link_Previous: '/girls&sports/2010-02-05/', Link_Next: '/girls&sports/2010-02-07/'}" + if get_what == "url": + splt = string.split(source, 'StripID:', 1) + splt = string.split(splt[1], ', ComicID:', 1) + url = "http://c0389161.cdn.cloudfiles.rackspacecloud.com/dyn/str_strip/" + splt[0] + ".full.gif" + return url + elif get_what == "next": + splt = string.split(source, "Link_Next: '", 1) + splt = string.split(splt[1], "'", 1) + splt = string.split(splt[0], "/") + if len(splt) < 3: + return False + else: + + return splt[2] + + elif get_what == "prev": + print "getting comics com prev: " + comicname + splt = string.split(source, "Link_Previous: '", 1) + splt = string.split(splt[1], "'", 1) + splt = string.split(splt[0], "/") + if len(splt) == 0: + return False + else: + if len(splt[2]) == 10: + return splt[2] + else: + return False + + + + + + + + # ------------UI class ComicHomePlugin(hildondesktop.HomePluginItem): - __gtype_name__ = 'ComicHomePlugin' + # __gtype_name__ = 'ComicHomePlugin' def __init__(self): hildondesktop.HomePluginItem.__init__(self) global supports_alpha self.set_settings(True) + self.active_comics = self.get_active_comics() + self.sizes = self.get_size_settings() + self.new_width = self.sizes['width'] + self.new_height = self.sizes['height'] 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() @@ -305,22 +1120,19 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): self.set_colormap(colormap) self.set_app_paintable(True) #setup internal area - self.set_size_request(480, 230) + self.set_size_request(self.sizes['width'], self.sizes['height']) self.imgvpos = 0; self.vbox = gtk.VBox() self.hbox = gtk.HBox() self.label = gtk.Label() self.label.set_alignment(xalign = 0, yalign = 0.5) self.label.set_use_markup(True) - titl = str(self.db.db[self.db.currentcomic]['title']) - if len(titl) > 22: - titl = titl[:19] + "..." - self.label.set_markup(' ' + self.comicname + ' ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '') + self.label.set_markup(self.get_markup()) self.e_goweb = gtk.EventBox() self.e_goweb.set_name('goweb') - self.e_goweb.set_size_request(220, 48) + self.e_goweb.set_size_request(self.sizes['width'] - 250, 48) # e_open is the eventbox for the image. Does nothing, but it's convenient to have it. # it will change to be a cairo thing when I have the time. self.e_open = gtk.EventBox() @@ -362,9 +1174,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) @@ -402,7 +1216,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): self.vbox.pack_end(self.hbox,False,False,0) self.hbox.pack_start(self.e_switch,False,False,0) - self.hbox.pack_start(self.e_goweb,False,False,10) + self.hbox.pack_start(self.e_goweb,False,False,5) self.hbox.pack_end(self.e_next,False,False,0) self.hbox.pack_end(self.e_prev,False,False,0) self.hbox.pack_end(self.e_down,False,False,0) @@ -410,9 +1224,11 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): self.vbox.show_all() self.add(self.vbox) + + # **************** 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 @@ -428,8 +1244,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): # Now we have a colormap appropriate for the screen, use it self.set_colormap(colormap) - - return False + hildondesktop.HomePluginItem.do_realize(self) def do_expose_event(widget, event): cr = widget.window.cairo_create() @@ -449,7 +1264,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): @@ -471,7 +1286,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): print "pressed: " + func if func == 'up': self.e_open.remove(self.comic_image) - self.imgvpos = self.imgvpos - 80 + self.imgvpos = self.imgvpos - ((self.sizes['height'] - 48) / 2.1) if self.imgvpos < 0: self.imgvpos = 0 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos) @@ -480,7 +1295,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): elif func == 'down': self.e_open.remove(self.comic_image) - self.imgvpos = self.imgvpos + 80 + self.imgvpos = self.imgvpos + ((self.sizes['height'] - 48) / 2.1) 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() @@ -496,7 +1311,7 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): titl = str(self.db.db[self.db.currentcomic]['title']) if len(titl) > 22: titl = titl[:19] + "..." - self.label.set_markup(' ' + self.comicname + ' ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '') + self.label.set_markup(self.get_markup()) self.e_goweb.add(self.label) self.e_goweb.show_all() @@ -511,26 +1326,41 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): titl = str(self.db.db[self.db.currentcomic]['title']) if len(titl) > 22: titl = titl[:19] + "..." - self.label.set_markup(' ' + self.comicname + ' ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '') + self.label.set_markup(self.get_markup()) self.e_goweb.add(self.label) self.e_goweb.show_all() elif func == 'switch': - self.keypointer = (self.keypointer + 1) % 3 + # 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" + #sys.stdout.close() + #sys.stdout = sys.__stdout__ + #sys.stderr.close() + #sys.stderr = sys.__stderr__ + 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: titl = titl[:19] + "..." - self.label.set_markup(' ' + self.comicname + ' ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + titl + '') + self.label.set_markup(self.get_markup()) 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() @@ -551,44 +1381,72 @@ 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. def get_resized_pixmap(self, filename, vpos): print "vpos: " + str(vpos) - - maxheight = 182 - + + width = int(self.sizes['width']) + 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, 480, maxheight) + 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 = 478 - new_height = int((478.0 / pixbuf.get_width()) * pixbuf.get_height()) + new_width = picwidth + new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height()) 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,478,maxheight,parent_buf,1,0) + 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 - scaled_buf.copy_area(0,0,478,scaled_buf.get_height(),parent_buf,1,tmpy) + scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy) self.imgvpos = 0 +# test = parent_buf.render_pixmap_and_mask(0) +# parent_buf.add_alpha(True,chr(255),chr(255),chr(255)) retimg.set_from_pixbuf(parent_buf) + print retimg.get_pixel_size() print "vpos after fixing image: " + str(self.imgvpos) return retimg @@ -611,9 +1469,73 @@ 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(defaultcomics) + dbf.close() + ret = defaultcomics + return ret + + + def get_markup(self): + if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20: + return ' ' + self.comicname + ' ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '' + else: + return ' ' + self.comicname + '' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '' + + + #check if settings file exists + + #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): + print "loading options dialog" 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) @@ -621,32 +1543,193 @@ class ComicHomePlugin(hildondesktop.HomePluginItem): about_button.set_alignment(0,0,0,0) about_button.connect("clicked", self.show_about) - dialog.vbox.pack_start(about_button, True, True, 0) + 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) + + 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) + # dialog.vbox.pack_start(size_button, True, True, 0) + print "show!" dialog.show_all() 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() 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