""" Based around KarambaVerseOfTheDay by Jan Finell but for those who like non-christian quotes. by Oskar Flordal KarambaVerseOfTheDay by Jan Finell ### LICENSE ######################################################## This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ################################################################### """ __author__ = 'Oskar Flordal' __version__ = '0.1' # # standard modules # import karamba,random import re, time, urllib, os import xmms.control # # variables & constants # class Var: """ Helper class for keeping runtime variables """ def __init__(self, **kws): self.__dict__ = kws VAR = Var(shown=False, utime=None, quote=[], author='', availFortunes =[], selectedFortunes=[]) #selectedFortunes is an aray with numbers to be used i availFortunes # [0 3] would use 0 and three for easy randomization CHAR_H = 15 QUOTEMAXLENGTH = "400" WIDGETWIDTH = 400 LINEHEIGHT = 14 QUOTE_BG_X, QUOTE_BG_Y = (0,15) QUOTE_COLOR = (5, 64, 78) QUOTE_URL = "http://www.quotationspage.com/data/1qotd.js" #QUOTE_URL = "http://votd.christ.com/biblevotd/votd-nas.js" def findAvail(): clean = [] dir = os.listdir('/usr/share/games/fortunes/') for x in dir: if x.find('.dat')!=-1: clean.append(x[:len(x)-4]) dir = os.listdir('/usr/share/games/fortunes/off/') for x in dir: if x.find('.dat')!=-1: clean.append('off/' + x[:len(x)-4]) return clean #Fetch quote from a sample daily quote page def getFortune(widget): """ try: soc = urllib.urlopen(QUOTE_URL) #open stream jsData = soc.read() #read the text soc.close() except Exception,e : print 'Error fetching quote from "%s" (%s)' % (URL, e) return ('','','') lines = jsData.split('\n') #divide to lines #interesting stuff is on line 4 in this case lines = lines[2:4] quote = lines[0].split('<')[1].split('>')[1] author = lines[1].split('<')[4].split('>')[1] return (quote, author) """ # print VAR.selectedFortunes try: nr = VAR.selectedFortunes[random.randint(0,len(VAR.selectedFortunes)-1)] except Exception,e: nr = 0 return (["No fortunes selected or found"],'none') # print nr # print "fortune -s -n 400 -a " + VAR.availFortunes[nr] try: f =os.popen("fortune -s -n " +QUOTEMAXLENGTH +" -a " + VAR.availFortunes[nr]) except Exception,e : print 'Fortune not found' return (['Error running fortune. \nCheck your path'],'none') quote = f.readlines() status = f.close() author = VAR.availFortunes[nr] print author return (quote,author) #This gets called when an item is clicked in the theme CONFIGURATION menu, #not the popup menus that you create. # key = the reference to the configuration key that was changed # value = the new value (true or false) that was selected def menuOptionChanged(widget, key, value): # if (key == "all"): # karamba.setMenuConfigOption(widget, "all", "0") # print "all" # for x in VAR.availFortunes : # karamba.setMenuConfigOption(widget, x, "1") # karamba.readMenuConfigOption(widget, "all") # VAR.selectFortunes = range(0,len(VAR.availFortunes)) # print "in" # else: VAR.selectedFortunes = [] for x in range(0,len(VAR.availFortunes)): #need the numbers if (karamba.readMenuConfigOption(widget, VAR.availFortunes[x])): VAR.selectedFortunes.append(x) print VAR.selectedFortunes # configchange = 1 # werethreading = 1 def initConfMenu(widget): for x in VAR.availFortunes: karamba.addMenuConfigOption(widget, x, "I want fortunes from " + x) # karamba.setMenuConfigOption(widget, x, "0") # # Karamba functions # def initWidget(widget): VAR.availFortunes = findAvail() initConfMenu(widget) VAR.selectedFortunes = [] for x in range(0,len(VAR.availFortunes)): #need the numbers if (karamba.readMenuConfigOption(widget, VAR.availFortunes[x])): VAR.selectedFortunes.append(x) _update(widget) # print VAR.availFortunes # karamba.setMenuConfigOption(widget, "all", "0") karamba.attachClickArea(widget, karamba.getThemeText(widget,'BACKGROUND')) widgetUpdated(widget) def widgetUpdated(widget): if VAR.utime is None or time.time()-VAR.utime > 80000: VAR.quote, VAR.author = getFortune(widget) VAR.utime = time.time() _update(widget) def meterClicked(widget, meter, button): # print "meter" if VAR.quote: if VAR.shown: _hide_quote(widget) VAR.quote, VAR.author = getFortune(widget) _update(widget) else: # _update(widget) _show_quote(widget) karamba.redrawWidget(widget) VAR.shown = not VAR.shown # # protected methods # def _update(widget): karamba.changeText(widget, karamba.getThemeText(widget, 'AUTHOR'), "Fortune: " + VAR.author) karamba.redrawWidget(widget) def _show_quote(widget): bgHeight = CHAR_H*1+4 bgW = karamba.createImage(widget, QUOTE_BG_X, QUOTE_BG_Y+4, 'images/details_bg.png') # for i in range(5, bgHeight, 8): # karamba.resizeImage(widget, bgW, 400, i) # karamba.redrawWidget(widget) nrlines = len(VAR.quote) karamba.resizeWidget(widget, WIDGETWIDTH, 20+nrlines*LINEHEIGHT) karamba.resizeImage(widget, bgW, WIDGETWIDTH,5+nrlines*LINEHEIGHT ) karamba.redrawWidget(widget) quoteW = karamba.createText(widget, QUOTE_BG_X+3, QUOTE_BG_Y+4, WIDGETWIDTH, bgHeight, '\n'.join(VAR.quote)) karamba.changeTextSize(widget, quoteW, 11) karamba.changeTextColor(widget, quoteW, *QUOTE_COLOR) VAR.quoteW = quoteW VAR.bgW = bgW def _hide_quote(widget): karamba.hideImage(widget, VAR.bgW) karamba.hideText(widget, VAR.quoteW) karamba.deleteImage(widget, VAR.bgW) karamba.deleteText(widget, VAR.quoteW) VAR.quoteW = VAR.bgW = None