e343ffb725faa638ca97d9bdc1ae58a600d4bebd
[comic-widget] / src / usr / lib / hildon-desktop / comicwidget.py
1 import gobject
2 import hildon
3 import hildondesktop
4 import gtk
5 import csv
6 import urllib2
7 import string
8 import os
9 import osso
10 import cairo
11 import datetime
12 import shutil
13 import sys
14 import socket
15
16 socket.setdefaulttimeout(30)
17
18 supports_alpha = False
19
20 # LOGGING!
21 # sys.stdout = open('/home/user/.comic-widget/output_log.txt', 'a')
22 # sys.stderr = open('/home/user/.comic-widget/error_log.txt', 'a')
23 # print "Start logging!"
24
25 # constants. dbfile is the location of the csv
26 # comiccache is the location of the images
27 APP_VERSION = "0.4.1-1"
28
29 basedbdir = "/opt/comic-widget/db/"
30 imagedir = "/opt/comic-widget/images/"
31 dbdir = "/home/user/.comic-widget/"
32 activecomics = dbdir + "activecomics.cfg"
33 comiccache = "/home/user/MyDocs/.comics/"
34 defaultcomics = ['xkcd','wulff','sinfest']
35 comics = {
36                 "9_chickweed_lanecomicscom":{"name":"9 Chickweed Lane","link":"http://comics.com/9_chickweed_lane/","start":"2010-02-01","dbfile":dbdir + "comicdb.9chickweedlane.csv"},
37                 "agnescomicscom":{"name":"Agnes","link":"http://comics.com/agnes/","start":"2010-02-01","dbfile":dbdir + "comicdb.agnes.csv"},
38                 "andy_cappcomicscom":{"name":"Andy Capp","link":"http://comics.com/andy_capp/","start":"2010-02-01","dbfile":dbdir + "comicdb.andycapp.csv"},
39                 "alley_oopcomicscom":{"name":"Alley Oop","link":"http://comics.com/alley_oop/","start":"2010-02-01","dbfile":dbdir + "comicdb.alleyoop.csv"},
40                 "arlonjaniscomicscom":{"name":"Arlo and Janis","link":'http://comics.com/arlo&janis/',"start":"2010-02-01","dbfile":dbdir + "comicdb.arlonjanis.csv"},
41                 "bccomicscom":{"name":"B.C.","link":"http://comics.com/bc/","start":"2010-02-01","dbfile":dbdir + "comicdb.bc.csv"},
42                 "ballard_streetcomicscom":{"name":"Ballard Street","link":"http://comics.com/ballard_street/","start":"2010-02-01","dbfile":dbdir + "comicdb.ballardstreet.csv"},
43                 "babyblues":{"name":"Babyblues","link":"http://www.babyblues.com/","start":"01/19/2010","dbfile":dbdir + "comicdb.babyblues.csv"},
44                 "bencomicscom":{"name":"Ben","link":'http://comics.com/ben/',"start":"2010-02-01","dbfile":dbdir + "comicdb.ben.csv"},
45                 "bettycomicscom":{"name":"Betty","link":"http://comics.com/betty/","start":"2010-02-01","dbfile":dbdir + "comicdb.betty.csv"},
46                 "big_natecomicscom":{"name":"Big Nate","link":"http://comics.com/big_nate/","start":"2010-02-01","dbfile":dbdir + "comicdb.bignate.csv"},
47                 "brevitycomicscom":{"name":"Brevity","link":"http://comics.com/brevity/","start":"2010-02-01","dbfile":dbdir + "comicdb.brevity.csv"},
48                 "candorvillecomicscom":{"name":"Candorville","link":"http://comics.com/candorville/","start":"2010-02-01","dbfile":dbdir + "comicdb.candorville.csv"},
49                 "cheap_thrillscomicscom":{"name":"Cheap Thrills","link":"http://comics.com/cheap_thrills/","start":"2010-02-01","dbfile":dbdir + "comicdb.cheapthrills.csv"},
50                 "committedcomicscom":{"name":"Committed","link":"http://comics.com/committed/","start":"2010-02-01","dbfile":dbdir + "comicdb.committed.csv"},
51                 "cownboycomicscom":{"name":"Cow and Boy","link":'http://comics.com/cow&boy/',"start":"2010-02-01","dbfile":dbdir + "comicdb.cownboy.csv"},
52                 "cyanide":{"name":"Cyanide and Happiness","link":"http://explosm.com/","start":"1920","dbfile":dbdir + "comicdb.cyanide.csv"},
53                 "daddyshomecomicscom":{"name":"Daddy's Home","link":"http://comics.com/daddys_home/","start":"2010-02-01","dbfile":dbdir + "comicdb.daddyshome.csv"},
54                 "dilbert":{"name":"Dilbert","link":"http://dilbert.com/","start":"2010-01-01","dbfile":dbdir + "comicdb.dilbert.csv"},
55                 "dog_eat_dougcomicscom":{"name":"Dog eat Doug","link":"http://comics.com/dog_eat_doug/","start":"2010-02-01","dbfile":dbdir + "comicdb.dogeatdoug.csv"},
56                 "drabblecomicscom":{"name":"Drabble","link":"http://comics.com/drabble/","start":"2010-02-01","dbfile":dbdir + "comicdb.drabble.csv"},
57                 "f_minuscomicscom":{"name":"F Minus","link":"http://comics.com/f_minus/","start":"2010-02-01","dbfile":dbdir + "comicdb.f_minus.csv"},
58                 "family_treecomicscom":{"name":"Family Tree","link":"http://comics.com/family_tree/","start":"2010-02-01","dbfile":dbdir + "comicdb.familytree.csv"},
59                 "farcuscomicscom":{"name":"Farcus","link":'http://comics.com/farcus/',"start":"2010-02-01","dbfile":dbdir + "comicdb.farcus.csv"},
60                 "fat_catscomicscom":{"name":"Fat Cats","link":'http://comics.com/fat_cats_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.fatcats.csv"},
61                 "ferdnandcomicscom":{"name":"Ferd'nand","link":'http://comics.com/ferdnand/',"start":"2010-02-01","dbfile":dbdir + "comicdb.ferdnand.csv"},
62                 "flight_deckcomicscom":{"name":"Flight_Deck","link":'http://comics.com/flight_deck/',"start":"2010-02-01","dbfile":dbdir + "comicdb.flightdeck.csv"},
63                 "flonfriendscomicscom":{"name":"Flo and Friends","link":'http://comics.com/flo&friends/',"start":"2010-02-01","dbfile":dbdir + "comicdb.flonfriends.csv"},
64                 "fort_knoxcomicscom":{"name":"Fort Knox","link":'http://comics.com/fort_knox/',"start":"2010-02-01","dbfile":dbdir + "comicdb.fortknox.csv"},
65                 "franknernestcomicscom":{"name":"Frank and Ernest","link":'http://comics.com/frank&ernest/',"start":"2010-02-01","dbfile":dbdir + "comicdb.franknernest.csv"},
66                 "frazzcomicscom":{"name":"Frazz","link":"http://comics.com/frazz/","start":"2010-02-01","dbfile":dbdir + "comicdb.frazz.csv"},
67                 "free_rangecomicscom":{"name":"Free Range","link":'http://comics.com/free_range/',"start":"2010-02-01","dbfile":dbdir + "comicdb.freerange.csv"},
68                 "geechcomicscom":{"name":"Geech","link":'http://comics.com/geech_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.geech.csv"},
69                 "getfuzzycomicscom":{"name":"Get Fuzzy","link":"http://comics.com/get_fuzzy/","start":"2010-02-01","dbfile":dbdir + "comicdb.getfuzzy.csv"},
70                 "girlsnsportscomicscom":{"name":"Girls and Sports","link":'http://comics.com/girls&sports/',"start":"2010-02-01","dbfile":dbdir + "comicdb.gitlsnsports.csv"},
71                 "graffiticomicscom":{"name":"Graffiti","link":'http://comics.com/graffiti/',"start":"2010-02-01","dbfile":dbdir + "comicdb.graffiti.csv"},
72                 "grand_avenuecomicscom":{"name":"Grand Avenue","link":'http://comics.com/grand_avenue/',"start":"2010-02-01","dbfile":dbdir + "comicdb.grandavenue.csv"},
73                 "heathcliffcomicscom":{"name":"Heathcliff","link":'http://comics.com/heathcliff/',"start":"2010-02-01","dbfile":dbdir + "comicdb.heathcliff.csv"},
74                 "herb_and_jamaalcomicscom":{"name":"Herb and Jamaal","link":'http://comics.com/herb_and_jamaal/',"start":"2010-02-01","dbfile":dbdir + "comicdb.herbandjamaal.csv"},
75                 "hermancomicscom":{"name":"Herman","link":'http://comics.com/herman/',"start":"2010-02-01","dbfile":dbdir + "comicdb.herman.csv"},
76                 "home_and_awaycomicscom":{"name":"Home and Away","link":'http://comics.com/home_and_away/',"start":"2010-02-01","dbfile":dbdir + "comicdb.homeandaway.csv"},
77                 "its_all_about_youcomicscom":{"name":"It's All About You","link":'http://comics.com/its_all_about_you/',"start":"2010-02-01","dbfile":dbdir + "comicdb.itsallaboutyou.csv"},
78                 "janes_worldcomicscom":{"name":"Jane's World","link":'http://comics.com/janes_world/',"start":"2010-02-01","dbfile":dbdir + "comicdb.janesworld.csv"},
79                 "jump_startcomicscom":{"name":"Jump Start","link":'http://comics.com/jump_start/',"start":"2010-02-01","dbfile":dbdir + "comicdb.jumpstart.csv"},
80                 "kit_n_carlylecomicscom":{"name":"Kit 'n' Carlyle","link":'http://comics.com/kit_n_carlyle/',"start":"2010-02-01","dbfile":dbdir + "comicdb.kitncarlyle.csv"},
81                 "lil_abnercomicscom":{"name":"Li'l Abner","link":'http://comics.com/lil_abner_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.lilabner.csv"},
82                 "liberty_meadowscomicscom":{"name":"Liberty Meadows","link":'http://comics.com/liberty_meadows/',"start":"2010-02-01","dbfile":dbdir + "comicdb.libertymeadows.csv"},
83                 "little_dog_lostcomicscom":{"name":"Little Dog Lost","link":'http://comics.com/little_dog_lost/',"start":"2010-02-01","dbfile":dbdir + "comicdb.littledoglost.csv"},
84                 "lolacomicscom":{"name":"Lola","link":'http://comics.com/lola/',"start":"2010-02-01","dbfile":dbdir + "comicdb.lola.csv"},
85                 "luanncomicscom":{"name":"Luann","link":'http://comics.com/luann/',"start":"2010-02-01","dbfile":dbdir + "comicdb.luann.csv"},
86                 "marmadukecomicscom":{"name":"Marmaduke","link":'http://comics.com/marmaduke/',"start":"2010-02-01","dbfile":dbdir + "comicdb.marmaduke.csv"},
87                 "megcomicscom":{"name":"Meg","link":'http://comics.com/meg_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.meg.csv"},
88                 "minimum_securitycomicscom":{"name":"Minimum Security","link":'http://comics.com/minimum_security/',"start":"2010-02-01","dbfile":dbdir + "comicdb.minimumsecurity.csv"},
89                 "moderately_confusedcomicscom":{"name":"Moderately Confused","link":'http://comics.com/moderately_confused/',"start":"2010-02-01","dbfile":dbdir + "comicdb.moderatelyconfused.csv"},
90                 "mommacomicscom":{"name":"Momma","link":'http://comics.com/momma/',"start":"2010-02-01","dbfile":dbdir + "comicdb.momma.csv"},
91                 "motleycomicscom":{"name":"Motley","link":'http://comics.com/motley_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.motley.csv"},
92                 "nancycomicscom":{"name":"Nancy","link":'http://comics.com/nancy/',"start":"2010-02-01","dbfile":dbdir + "comicdb.nancy.csv"},
93                 "natural_selectioncomicscom":{"name":"Natural Selection","link":'http://comics.com/natural_selection/',"start":"2010-02-01","dbfile":dbdir + "comicdb.naturalselection.csv"},
94                 "nest_headscomicscom":{"name":"Nest Heads","link":'http://comics.com/nest_heads/',"start":"2010-02-01","dbfile":dbdir + "comicdb.nestheads.csv"},
95                 "off_the_markcomicscom":{"name":"Off the mark","link":"http://comics.com/off_the_mark/","start":"2010-02-01","dbfile":dbdir + "comicdb.offthemark.csv"},
96                 "on_a_claire_daycomicscom":{"name":"On A Claire Day","link":'http://comics.com/on_a_claire_day/',"start":"2010-02-01","dbfile":dbdir + "comicdb.onaclaireday.csv"},
97                 "one_big_happycomicscom":{"name":"One Big Happy","link":'http://comics.com/one_big_happy_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.onebighappy.csv"},
98                 "over_the_hedgecomicscom":{"name":"Over the Hedge","link":'http://comics.com/over_the_hedge/',"start":"2010-02-01","dbfile":dbdir + "comicdb.overthehedge.csv"},
99                 "pc_and_pixelcomicscom":{"name":"PC and Pixel","link":'http://comics.com/pc_and_pixel/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pcandpixel.csv"},
100                 "peanutscomicscom":{"name":"Peanuts","link":"http://comics.com/peanuts/","start":"2010-02-01","dbfile":dbdir + "comicdb.peanuts.csv"},
101                 "pearls_before_swinecomicscom":{"name":"Pearls Before Swine","link":'http://comics.com/pearls_before_swine/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pearlsbeforeswine.csv"},
102                 "phd":{"name":"PHD Comics","link":"http://www.phdcomics.com/","start":1240,"dbfile":dbdir + "comicdb.phd.csv"},
103                 "picklescomicscom":{"name":"Pickles","link":'http://comics.com/pickles/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pickles.csv"},
104                 "prickly_citycomicscom":{"name":"Prickly City","link":'http://comics.com/prickly_city/',"start":"2010-02-01","dbfile":dbdir + "comicdb.pricklycity.csv"},
105                 "raising_duncancomicscom":{"name":"Raising Duncan","link":'http://comics.com/raising_duncan_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.raisingduncan.csv"},
106                 "reality_checkcomicscom":{"name":"Reality Check","link":'http://comics.com/reality_check/',"start":"2010-02-01","dbfile":dbdir + "comicdb.realitycheck.csv"},
107                 "rednrovercomicscom":{"name":"Red and Rover","link":'http://comics.com/red&rover/',"start":"2010-02-01","dbfile":dbdir + "comicdb.rednrover.csv"},
108                 "rip_haywirecomicscom":{"name":"Rip Haywire","link":'http://comics.com/rip_haywire/',"start":"2010-02-01","dbfile":dbdir + "comicdb.riphaywire.csv"},
109                 "ripleys_believe_it_or_notcomicscom":{"name":"Ripley's Believe it or not","link":'http://comics.com/ripleys_believe_it_or_not/',"start":"2010-02-01","dbfile":dbdir + "comicdb.ripleysbelieveitornot.csv"},
110                 "rose_is_rosecomicscom":{"name":"Rose Is Rose","link":'http://comics.com/rose_is_rose/',"start":"2010-02-01","dbfile":dbdir + "comicdb.roseisrose.csv"},
111                 "rubescomicscom":{"name":"Rubes","link":'http://comics.com/rubes/',"start":"2010-02-01","dbfile":dbdir + "comicdb.rubes.csv"},
112                 "rudy_parkcomicscom":{"name":"Rudy Park","link":'http://comics.com/rudy_park/',"start":"2010-02-01","dbfile":dbdir + "comicdb.rudypark.csv"},
113                 "scary_garycomicscom":{"name":"Scary Gary","link":'http://comics.com/scary_gary/',"start":"2010-02-01","dbfile":dbdir + "comicdb.scarygary.csv"},
114                 "shirley_and_soncomicscom":{"name":"Shirley and Son","link":'http://comics.com/shirley_and_son_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.shirleyandson.csv"},
115                 "sinfest":{"name":"Sinfest","link":"http://sinfest.com/","start":3400,"dbfile":dbdir + "comicdb.sinfest.csv"},
116                 "soup_to_nutzcomicscom":{"name":"Soup to Nutz","link":'http://comics.com/soup_to_nutz/',"start":"2010-02-01","dbfile":dbdir + "comicdb.souptonutz.csv"},
117                 "speed_bumpcomicscom":{"name":"Speed Bump","link":'http://comics.com/speed_bump/',"start":"2010-02-01","dbfile":dbdir + "comicdb.speedbump.csv"},
118                 "spot_the_frogcomicscom":{"name":"Spot the Frog","link":'http://comics.com/spot_the_frog/',"start":"2010-02-01","dbfile":dbdir + "comicdb.spotthefrog.csv"},
119                 "state_of_the_unioncomicscom":{"name":"State of the Union","link":'http://comics.com/state_of_the_union/',"start":"2010-02-01","dbfile":dbdir + "comicdb.stateoftheunion.csv"},
120                 "strange_brewcomicscom":{"name":"Strange Brew","link":'http://comics.com/strange_brew/',"start":"2010-02-01","dbfile":dbdir + "comicdb.strangebrew.csv"},
121                 "tarzancomicscom":{"name":"Tarzan","link":'http://comics.com/tarzan_classics/',"start":"2010-02-01","dbfile":dbdir + "comicdb.tarzan.csv"},
122                 "thats_lifecomicscom":{"name":"That's Life","link":'http://comics.com/thats_life/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thatslife.csv"},
123                 "the_barncomicscom":{"name":"The Barn","link":'http://comics.com/the_barn/',"start":"2010-02-01","dbfile":dbdir + "comicdb.the_barn.csv"},
124                 "the_born_losercomicscom":{"name":"The Born Loser","link":'http://comics.com/the_born_loser/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thebornloser.csv"},
125                 "the_bucketscomicscom":{"name":"The Buckets","link":'http://comics.com/the_buckets/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thebuckets.csv"},
126                 "the_dinette_setcomicscom":{"name":"The Dinette Set","link":'http://comics.com/the_dinette_set/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thedinetteset.csv"},
127                 "the_grizzwellscomicscom":{"name":"The Grizzwells","link":'http://comics.com/the_grizzwells/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thegrizzwells.csv"},
128                 "the_humble_stumblecomicscom":{"name":"The Humble Stumble","link":'http://comics.com/the_humble_stumble/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thehumblestumble.csv"},
129                 "the_knight_lifecomicscom":{"name":"The Knight Life","link":'http://comics.com/the_knight_life/',"start":"2010-02-01","dbfile":dbdir + "comicdb.theknightlife.csv"},
130                 "the_meaning_of_lilacomicscom":{"name":"The Meaning of Lila","link":'http://comics.com/the_meaning_of_lila/',"start":"2010-02-01","dbfile":dbdir + "comicdb.themeaningoflila.csv"},
131                 "the_other_coastcomicscom":{"name":"The Other Coast","link":'http://comics.com/the_other_coast/',"start":"2010-02-01","dbfile":dbdir + "comicdb.theothercoast.csv"},
132                 "the_sunshine_clubcomicscom":{"name":"the Sunshine Club","link":'http://comics.com/the_sunshine_club/',"start":"2010-02-01","dbfile":dbdir + "comicdb.thesunshineclub.csv"},
133                 "unstrange_phenomenacomicscom":{"name":"Unstrange Phenomena","link":'http://comics.com/unstrange_phenomena/',"start":"2010-02-01","dbfile":dbdir + "comicdb.unstrangephenomena.csv"},
134                 "watch_your_headcomicscom":{"name":"Watch Your Head","link":'http://comics.com/watch_your_head/',"start":"2010-02-01","dbfile":dbdir + "comicdb.watchyourhead.csv"},
135                 "wizard_of_idcomicscom":{"name":"Wizard of Id","link":'http://comics.com/wizard_of_id/',"start":"2010-02-01","dbfile":dbdir + "comicdb.wizardofid.csv"},
136                 "working_dazecomicscom":{"name":"Working Daze","link":'http://comics.com/working_daze/',"start":"2010-02-01","dbfile":dbdir + "comicdb.workingdaze.csv"},
137                 "working_it_outcomicscom":{"name":"Working It Out","link":'http://comics.com/working_it_out/',"start":"2010-02-01","dbfile":dbdir + "comicdb.workingitout.csv"},
138                 "wulff":{"name":"Wulffmorgenthaler","link":"http://wulffmorgenthaler.com/","start":"edd3411b-96ca-4d93-bd5f-0cf1deb67c8a","dbfile":dbdir + "comicdb.wulff.csv"},
139                 "xkcd":{"name":"xkcd","link":"http://xkcd.org/","start":666,"dbfile":dbdir + "comicdb.xkcd.csv"},
140                 "zack_hillcomicscom":{"name":"Zack Hill","link":'http://comics.com/zack_hill/',"start":"2010-02-01","dbfile":dbdir + "comicdb.zackhill.csv"},
141
142
143
144                 }
145 defaults = {'width':480,'height':230}
146 #defaults = {'width':480,'height':240}
147 sizefile = "/home/user/.comic-widget/size.cfg"
148 previous = False
149 next = False
150
151
152 # handling of the comics
153 class ComicDb():
154         def __init__(self, comic):
155                 self.comic = comic
156                 self.start = comics[self.comic]["start"]
157                 self.dbfile = comics[self.comic]["dbfile"]
158
159                 #if db file exist, read it
160                 # if os.path.isfile(self.dbfile) == True:
161                 #       dbf = open(self.dbfile, 'r')
162                 # if not, create it.
163                 dbf = self.get_dbfile()
164                 dbr = csv.DictReader(dbf)
165                 self.db = []
166                 for row in dbr:
167                         self.db.insert(0,row)
168                 dbf.close()
169                 if len(self.db) == 0:
170                         self.refresh()
171                 self.currentcomic = 0
172
173
174
175         def get_dbfile(self):
176                 #if db file exist, read it
177                 print "fetching " + self.dbfile
178                 if os.path.isfile(self.dbfile) == True:
179                         print "found " + self.dbfile
180                         dbf = open(self.dbfile, 'r')
181                         return dbf
182                 # if not, copy it.
183                 else:
184                         print "file " + self.dbfile + " not found"
185                         if not os.path.exists(dbdir):
186                                 print "path not found: " + dbdir
187                                 try:
188                                         os.makedirs(dbdir)
189                                 except:
190                                         print "comic db creation failed on mkdir"
191                                         exit()
192                         try:
193                                 shutil.copyfile(basedbdir + "comicdb." + self.comic + ".csv", dbdir + "comicdb." + self.comic + ".csv")
194                         except:
195                                 print "comic db creation failed on copy"
196                                 print "creating new..."
197                                 dbf = open(self.dbfile, 'w')
198                                 dbf.write('comic,id,link,url,filename,title\n')
199                                 dbf.close()
200                                 
201                         if os.path.isfile(self.dbfile) == True:
202                                 dbf = open(self.dbfile, 'r')
203                                 return dbf
204                         else:
205                                 print "comic db creation failed after copy"
206
207
208         def get_comic(self):
209                 print str(self.currentcomic)
210                 if self.currentcomic < 0:
211                         self.currentcomic = 0
212                         if len(self.db) > 0:
213                                 fetchid = self.db[self.currentcomic]['id']
214                         else:
215                                 fetchid = comics[self.comic]['start']
216                         self.fetch_newer(self.comic, fetchid)
217                         self.refresh()
218                 if  len(self.db) < (self.currentcomic + 1):
219                         print "Empty db"
220                         self.currentcomic = len(self.db) - 1
221                         if len(self.db) > 0:
222                                 fetchid = self.db[self.currentcomic]['id']
223                         else:
224                                 fetchid = comics[self.comic]['start']
225                         self.fetch_earlier(self.comic, fetchid)
226                         self.refresh()
227                         self.currentcomic = len(self.db) - 1
228                         
229                 dbrow = self.db[self.currentcomic]
230                 filename = comiccache + self.comic + "/" + dbrow['filename']
231                 print filename + "\n"
232                 
233                 if os.path.isfile(filename):
234                         print "found, returning " + filename + "\n"
235                         return filename
236                 else:
237                         #fetch file
238                         print filename + " not found\nretrieving " + dbrow['url'] + "\n"
239                         if not os.path.exists(comiccache + self.comic + "/"):
240                                 print "create dir " + comiccache + self.comic + "/"
241                                 try:
242                                         os.makedirs(comiccache + self.comic + "/")
243                                         print "created dir"
244                                 except:
245                                         print "comic db creation failed on mkdir"
246                         try:
247                                 f = open(filename, "wb")
248                                 print "writing to " + filename
249                                 req = urllib2.Request(dbrow['url'])
250                                 req.add_header('Referer', dbrow['link'])
251                                 tmpimg = urllib2.urlopen(req)
252                                 f.write(tmpimg.read())
253                                 tmpimg = None
254                                 f.close()
255
256 #                               urllib.urlretrieve(dbrow['url'], filename)
257                         except:
258                                 if os.path.isfile(filename):
259                                         os.remove(filename)
260                         print "success\n"
261                         return filename
262
263         def get_current_row(self):
264                 return self.db[self.currentcomic]               
265
266         def get_link(self):
267                 print str(self.currentcomic)
268                 if self.currentcomic < 0:
269                         self.refresh()
270                         self.currentcomic = 0
271                 if  len(self.db) < (self.currentcomic + 1):
272                         self.currentcomic -= 1
273                 return self.db[self.currentcomic]['link']
274
275
276
277         def insert_row(self, irow):
278                 dbf = open(self.dbfile, 'a')
279                 dbw = csv.writer(dbf)
280                 dbw.writerow(irow)
281                 dbf.close()
282
283         def insert_row_first(self, irow):
284                 dbf = open(self.dbfile, 'w')
285                 dbf.write('comic,id,link,url,filename,title\n')
286                 dbw = csv.writer(dbf)
287                 dbw.writerow(irow)
288                 dbf.close()
289                 dbf = open(self.dbfile, 'a')
290                 dbw = csv.DictWriter(dbf, ['comic','id','link','url','filename','title'])
291                 tmpdb = self.db
292                 tmpdb.reverse()
293                 for row in tmpdb:
294                         dbw.writerow(row)
295                 dbf.close()
296
297         def refresh(self):
298         #       if len(self.db) < 1:
299         #               self.currentcomic = -1
300         #               self.fetch_latest_std(self.comic, self.start)
301         #       elif self.currentcomic == 0 or self.currentcomic < 0:
302         #               self.fetch_latest_std(self.comic, self.db[0]['id'])
303         #       elif self.currentcomic == (len(self.db) - 1):
304         #               self.fetch_earlier(self.comic, self.db[self.currentcomic]['id'])
305                 dbf = open(self.dbfile, 'r')
306                 dbr = csv.DictReader(dbf)
307                 self.db = []
308                 for row in dbr:
309                         self.db.insert(0,row)
310                 dbf.close()
311                 if len(self.db) == 0:
312                         self.fetch_earlier(self.comic, self.start)
313                         dbf = open(self.dbfile, 'r')
314                         dbr = csv.DictReader(dbf)
315                         self.db = []
316                         for row in dbr:
317                                 self.db.insert(0,row)
318                         dbf.close()
319
320
321 # fetch earlier
322         def fetch_earlier(self, comic, earliest):
323                 print "fetch before, " + comic + " earliest"
324                 print comic[-9:]
325                 if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
326                         print "getting get_prev_id..."
327                         comicid = self.get_prev_id(comic,earliest)
328                         if not comicid:
329                                 print "already at first comic"
330                                 return
331                         print "got " + comicid + " as the one before current..."
332                 elif len(str(earliest)) == 10:
333                         # date id.
334                         dt = string.split(earliest, "-")
335                         d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
336                         earlier = d - datetime.timedelta( 1 )
337                         comicid = earlier.isoformat()
338                 else:
339                         comicid = int(earliest) - 1
340
341                 irow = self.get_irow(comic, comicid)
342                 if irow and irow[0] == "skip":
343                                 print "problem with this one, fetching " + str(irow[2]) + " instead."
344                                 irow = self.get_irow(comic, irow[2])
345
346                 if irow and len(irow) > 3:
347                         print "got irow: "
348                         print irow
349                         print "\ninserting...\n"
350                         self.insert_row_first([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
351                 else:
352                         print "No comic found at " + comicid
353
354
355
356
357
358         def fetch_newer(self, comic, newest):
359                 if comic == "cyanide" or comic == "wulff" or comic == "babyblues" or comic[-9:] == "comicscom":
360                         comicid = self.get_next_id(comic,newest)
361                         if not comicid:
362                                 print "already at last comic"
363                                 return
364                 elif len(newest) == 10:
365                         # date id.
366                         dt = string.split(newest, "-")
367                         d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
368                         newest = d + datetime.timedelta( 1 )
369                         comicid = newest.isoformat()
370                 else:
371                         comicid = int(newest) + 1
372
373                 irow = self.get_irow(comic, comicid)
374                 if irow and irow[0] == "skip":
375                                 print "problem with this one, fetching " + str(irow[1]) + " instead."
376                                 irow = self.get_irow(comic, irow[1])
377
378                 if irow and len(irow) > 3:
379                         print "got irow: "
380                         print irow
381                         print "\ninserting...\n"
382                         self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
383                 else:
384                         print "No comic found at " + str(comicid)
385
386
387
388
389         def fetch_latest_std(self, comic, latest):
390                 print "fetching new after " + str(comic) + " " + str(latest)
391                 next = False
392                 dateid = False
393                 if comic == 'cyanide':
394                         next = self.get_next_id(comic, latest)
395                         if not next:
396                                 return
397                         else:
398                                 comicid = next
399                 else: 
400                         if len(str(latest)) == 10:
401                                 # date id.
402                                 dateid = True
403                                 dt = string.split(latest, "-")
404                                 d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
405                                 newer = d + datetime.timedelta( 1 )
406                                 comicid = newer.isoformat()
407                         else:
408                                 comicid = int(latest) + 1
409
410                 if len(self.db) > 0:
411                         lasturl = self.db[0]['url']
412                 else:
413                         lasturl = "http"
414                 while True:
415                         irow = self.get_irow(comic, comicid)
416                         if irow:
417                                 print "got irow: " + str(irow)
418                                 if str(irow[0]) == 'skip':
419                                         print "skipping this one..."
420                                         next = int(irow[1])
421                                 else:
422                                         if irow[3] == lasturl:
423                                                 print "Looping the same, break break break!"
424                                                 break
425                                         lasturl = irow[3]
426                                         print "inserting..."
427                                         self.insert_row([irow[0],irow[1],irow[2],irow[3],irow[4],irow[5]])
428                                         if len(irow) > 6:
429                                                 next = irow[6]
430                                                 if not next:
431                                                         break
432                                 if dateid:
433                                         dt = string.split(comicid, "-")
434                                         d = datetime.date(int(dt[0]),int(dt[1]),int(dt[2]))
435                                         newer = d + datetime.timedelta( 1 )
436                                         comicid = newer.isoformat()
437                                 elif next:
438                                         comicid = next
439                                 else:
440                                         comicid += 1
441                         else:
442                                 break
443
444
445
446
447         def get_next_id(self, comic, number):
448                 if comic == 'babyblues':
449                         link = "http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=" + str(number)
450                         print "link: " + link
451                         try:
452                                 f = urllib2.urlopen(link)
453                                 hcode = f.code
454                         except:
455                                 hcode = 404
456                         print "got hcode = " + str(hcode) + "\n"
457                         if (hcode != 200):
458                                 return False
459                         else:
460                                 print "checking next Babyblues date.."
461                                 s = f.read()
462                                 f.close()
463                                 # title:
464                                 splt = string.split(s, 'nextStripLink', 1)
465                                 if len(splt) < 2:
466                                         print "no 'prev' found"
467                                         return False
468                                 else:
469                                         next = splt[1][48:58]
470                                         print "got next: " + next
471
472                                         return next
473
474                 if comic == 'cyanide':
475                         link = "http://www.explosm.net/comics/" + str(number) + "/"
476                         print "link: " + link
477                         try:
478                                 f = urllib2.urlopen(link)
479                                 hcode = f.code
480                         except:
481                                 hcode = 404
482                         print "got hcode = " + str(hcode) + "\n"
483                         if (hcode != 200):
484                                 return False
485                         else:
486                                 # print "Cyanide & Happiness is unreliable, so we need to track next and prev"
487                                 s = f.read()
488                                 f.close()
489                                 # title:
490                                 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
491                                 if len(splt) < 2:
492                                         print "no 'next' found"
493                                         return False
494                                 splt = string.split(splt[1], '/">Next >', 1)
495                                 if len(splt) < 2:
496                                         print "no 'next' found"
497                                         return False
498                                 else:
499                                         print "got next: " + splt[0]
500                                         return splt[0]  
501                 if comic == 'wulff':
502                         link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
503                         print "link: " + link
504                         try:
505                                 f = urllib2.urlopen(link)
506                                 hcode = f.code
507                         except:
508                                 hcode = 404
509                         print "got hcode = " + str(hcode) + "\n"
510                         if (hcode != 200):
511                                 return False
512                         else:
513                                 s = f.read()
514                                 f.close()
515                                 # title:
516                                 splt = string.split(s, '<a href="/default.aspx?id=', 1)
517                                 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
518                                         print "no previous found"
519                                         if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
520                                                 print "no next found!"
521                                                 return False
522                                 else:
523                                         splt = string.split(splt[1], '<a href="/default.aspx?id=', 1)
524                                 if len(splt) < 2:
525                                         print "no next found! At newest comic!"
526                                         return False
527                                 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aNext"':
528                                         print "no next found!"
529                                         return False
530                                 else:
531                                         return splt[1][:36]
532                 elif comic[-9:] == "comicscom":
533                         link = comics[comic]['link'] + str(number) + "/"
534                         print "link: " + link
535                         try:
536                                 f = urllib2.urlopen(link)
537                                 hcode = f.code
538                         except:
539                                 hcode = 404
540                         print "got hcode = " + str(hcode) + "\n"
541                         if (hcode != 200):
542                                 return False
543                         else:
544                                 s = f.read()
545                                 f.close()
546                                 return self.parse_comics_com(comic, s, 'next')
547
548
549
550
551
552
553         def get_prev_id(self, comic, number):
554                 if comic == 'babyblues':
555                         link = 'http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=' + str(number)
556                         print "link: " + link
557                         try:
558                                 f = urllib2.urlopen(link)
559                                 hcode = f.code
560                         except:
561                                 hcode = 404
562                         print "got hcode = " + str(hcode) + "\n"
563                         if (hcode != 200):
564                                 return False
565                         else:
566                                 print "checking prev Babyblues date.."
567                                 s = f.read()
568                                 f.close()
569                                 # title:
570                                 splt = string.split(s, 'prevStripLink', 1)
571                                 if len(splt) < 2:
572                                         print "no 'prev' found"
573                                         exit()
574                                         return False
575                                 else:
576                                         prev = splt[1][48:58]
577                                         print "got previous: " + prev
578                                         return prev
579
580                 if comic == 'cyanide':
581                         link = "http://www.explosm.net/comics/" + str(number) + "/"
582                         print "link: " + link
583                         try:
584                                 f = urllib2.urlopen(link)
585                                 hcode = f.code
586                         except:
587                                 hcode = 404
588                         print "got hcode = " + str(hcode) + "\n"
589                         if (hcode != 200):
590                                 return False
591                         else:
592                                 print "Cyanide & Happiness is unreliable, so we need to track next and prev"
593                                 s = f.read()
594                                 f.close()
595                                 # title:
596                                 splt = string.split(s, 'First</a> | <a href="/comics/', 1)
597                                 if len(splt) < 2:
598                                         print "no 'prev' found"
599                                         return False
600                                 splt = string.split(splt[1], '/">< Previous', 1)
601                                 if len(splt) < 2 or len(splt[0]) > 5:
602                                         print "no 'next' found"
603                                         return False
604                                 else:
605                                         print "got previous: " + splt[0]
606                                         return splt[0]  
607                 elif comic == 'wulff':
608                         link = "http://wulffmorgenthaler.com/default.aspx?id=" + number
609                         print "link: " + link
610                         try:
611                                 f = urllib2.urlopen(link)
612                                 hcode = f.code
613                         except:
614                                 hcode = 404
615                         print "got hcode = " + str(hcode) + "\n"
616                         if (hcode != 200):
617                                 return False
618                         else:
619                                 s = f.read()
620                                 f.close()
621                                 # title:
622                                 splt = string.split(s, '<a href="/default.aspx?id=', 1)
623                                 if not splt[1][36:69] == '" id="ctl00_content_Strip1_aPrev"':
624                                         print splt[1][36:69]
625                                         print splt[1][:36]
626                                         print "no previous found"
627                                         return False
628                                 else:
629                                         return splt[1][:36]
630                 elif comic[-9:] == "comicscom":
631                         print "fetch prev, still in function..."
632                         link = comics[comic]['link'] + str(number) + "/"
633                         print "link: " + link
634                         try:
635                                 f = urllib2.urlopen(link)
636                                 hcode = f.code
637                         except:
638                                 hcode = 404
639                         print "got hcode = " + str(hcode) + "\n"
640                         if (hcode != 200):
641                                 return False
642                         else:
643                                 s = f.read()
644                                 f.close()
645                                 return self.parse_comics_com(comic, s, 'prev')
646
647
648
649
650         def get_irow(self, comic, number):
651                 dateid = False
652                 if len(str(number)) == 10:
653                         dateid = True
654                 else:
655                         if number < 0:
656                                 return False
657                         print "number is now: " + str(number) + "\n"
658                 if comic == 'xkcd':
659                         link = "http://xkcd.org/" + str(number) + "/"
660                 elif comic == 'sinfest':
661                         link = "http://www.sinfest.net/archive_page.php?comicID=" + str(number)
662                 elif comic == 'dilbert':
663                         link = "http://dilbert.com/" + str(number) + "/"
664                 elif comic == 'phd':
665                         link = "http://www.phdcomics.com/comics/archive.php?comicid=" + str(number)
666                 elif comic == 'cyanide':
667                         link = "http://www.explosm.net/comics/" + str(number) + "/"
668                 elif comic == 'wulff':
669                         link = "http://wulffmorgenthaler.com/default.aspx?id=" + str(number)
670                 elif comic == 'babyblues':
671                         link = "http://www.babyblues.com/archive/index.php?formname=getstrip&GoToDay=" + str(number)
672                 elif comic[-9:] == 'comicscom': 
673                         link = comics[comic]['link'] + str(number) + "/"
674                 else:
675                         return False
676
677                 print "link: " + link
678                 try:
679                         f = urllib2.urlopen(link)
680                         hcode = f.code
681                 except:
682                         hcode = 404
683                 print "got hcode = " + str(hcode) + "\n"
684                 if (hcode != 200):
685                         return False
686                 else:
687                         # Build db entry.
688                         # they look like: comic,id,link,url,filename,title
689                         # We already have comic, number/id and link
690
691                         if comic == 'xkcd':
692                                 s = f.read()
693                                 f.close()
694                                 # This should be done with regex but...
695                                 splt = string.split(s, 'png" title="', 1)
696                                 splt = string.split(splt[1], '" alt="', 1)
697                                 title = splt[0]
698                                 splt = string.split(splt[1], "<h3>Image URL (for hotlinking/embedding): ", 1)
699                                 splt = string.split(splt[1], "</h3>", 1)
700                                 url = splt[0]
701
702
703                         elif comic == 'sinfest':
704                                 s = f.read()
705                                 f.close()
706                                 splt = string.split(s, 'height="107"', 1)
707                                 splt = string.split(splt[1], '<img src="', 1)
708                                 splt = string.split(splt[1], '" alt="', 1)
709                                 url = splt[0]
710                                 if len(url) < 49:
711                                         print "Fake 404! Break break break!"
712                                         return False
713                                 splt = string.split(splt[1], '" border="0" />', 1)
714                                 title = splt[0]
715
716                         elif comic == 'dilbert':
717                                 s = f.read()
718                                 f.close()
719                                 splt = string.split(s, 'input type="hidden" name="PrintPath" value="', 1)
720                                 splt = string.split(splt[1], '" />', 1)
721                                 url = "http://dilbert.com" + splt[0]
722                                 if len(url) < 50:
723                                         print "Fake 404! Break break break!"
724                                         return False
725                                 title = ""
726
727                         elif comic == 'phd':
728                                 s = f.read()
729                                 f.close()
730                                 # title:
731                                 splt = string.split(s, '<title>PHD Comics: ', 1)
732                                 splt = string.split(splt[1], '</title>', 1)
733                                 title = splt[0];
734                                 splt = string.split(splt[1], '<td bgcolor=#FFFFFF align=center><img src=', 1)
735                                 splt = string.split(splt[1], ' align=top>', 1)
736                                 url = splt[0]
737                                 if len(url) < 49:
738                                         print "Fake 404! Break break break!"
739                                         return False
740
741                         elif comic == 'cyanide':
742                                 s = f.read()
743                                 f.close()
744                                 # title:
745                                 splt = string.split(s, ' First</a> | <a href="/comics/', 1)
746                                 if len(splt) < 2:
747                                         print "first comic?"
748                                         prev = False
749                                 else:
750                                         splt = string.split(splt[1], '/">< ', 1)
751                                         prev = splt[0];
752                                         print "got prev: " + prev
753                                 splt = string.split(s, 'Previous</a> | <a href="/comics/', 1)
754                                 if len(splt) < 2:
755                                         print "last comic?"
756                                         next = False
757                                 else:
758                                         splt = string.split(splt[1], '/">Next ></a>', 1)
759                                         if len(splt[0]) > 10:
760                                                 next = False
761                                         else:
762                                                 next = splt[0]
763                                                 print "got next: " + next
764
765                                 splt = string.split(s, '</tr><tr><td colspan=2>', 1)
766                                 if len(splt) < 2:
767                                         print "no comic?"
768                                         return False
769                                 splt = string.split(splt[1], ' <b>by <a href="', 1)
770                                 title = splt[0];
771                                 if len(title) > 10:
772                                         print "title wrong..."
773                                         return False
774                                 splt = string.split(splt[1], 'a daily webcomic" src="http://www.explosm', 1)
775                                 if len(splt) < 2:
776                                         print "a video? Try skipping"
777                                         return ['skip',next,prev]
778                                         
779                                 splt = string.split(splt[1], '">', 1)
780                                 url = "http://www.explosm" + splt[0]
781                                 splt2 = string.rsplit(url, "/", 1)
782                                 filename = splt2[1]
783                                 irow = [comic,number,link,url,filename,title,next,prev]
784                                 return irow
785
786
787                         elif comic == 'babyblues':
788                                 # babyblues is type .gif
789                                 s = f.read()
790                                 f.close()
791                                 splt = string.split(s, 'http://est.rbma.com/content/Baby_Blues?date=', 1)
792                                 flnm = splt[1][:8]
793                                 # check filename...
794                                 datesplt = string.split(number, "/")
795                                 flncheck = datesplt[2] + datesplt[0] + datesplt[1]
796                                 if not flnm == flncheck:
797                                         print "incorrect filename, end of line. Break break break!"
798                                         return False
799                                 url = 'http://est.rbma.com/content/Baby_Blues?date=' + flnm
800                                 title = number + " (2 weeks delay)"
801                                 filename = flnm + ".gif"
802                                 irow = [comic,number,link,url,filename,title]
803                                 return irow
804
805
806                         elif comic == 'wulff':
807                                 # wulf is type .gif
808                                 s = f.read()
809                                 f.close()
810                                 splt = string.split(s, '<img src="pics/dates/month/', 1)
811                                 if not splt[1][1:2] == ".":
812                                         title = splt[1][:2]
813                                 else:
814                                         title = "0" + splt[1][:1]
815                                 splt = string.split(splt[1], '<img src="pics/dates/day/', 1)
816                                 if not splt[1][1:2] == ".":
817                                         title = title + "-" + splt[1][:2]
818                                 else:
819                                         title = title + "-0" + splt[1][:1]
820                                 splt = string.split(splt[1], '<img src="pics/dates/year/', 1)
821                                 title = splt[1][:4] + "-" + title
822                                 # title done...
823                                 splt = string.split(splt[1], 'id="ctl00_content_Strip1_imgStrip" class="strip" src="striphandler.ashx?stripid=', 1)
824                                 number = splt[1][:36]
825                                 url = "http://wulffmorgenthaler.com/striphandler.ashx?stripid=" + number
826                                 filename = number + ".gif"
827                                 irow = [comic,number,link,url,filename,title]
828                                 return irow
829                         elif comic[-9:] == 'comicscom':
830                                 url = self.parse_comics_com(comic, f.read(), 'url') 
831                                 f.close()
832                                 title = number
833
834                         splt2 = string.rsplit(url, "/", 1)
835                         filename = splt2[1]
836                         try:
837                                 if filename == self.db[0]['filename']:
838                                         print "already exists! Break break break!"
839                                         return False
840                         except:
841                                 pass
842                         irow = [comic,number,link,url,filename,title]
843                         return irow
844
845
846         def parse_comics_com(self, comicname, source, get_what):
847                 #       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/'}"
848                 if get_what == "url":
849                         splt = string.split(source, 'StripID:', 1)
850                         splt = string.split(splt[1], ', ComicID:', 1)
851                         url = "http://c0389161.cdn.cloudfiles.rackspacecloud.com/dyn/str_strip/" + splt[0] + ".full.gif"
852                         return url
853                 elif get_what == "next":
854                         splt = string.split(source, "Link_Next: '", 1)
855                         splt = string.split(splt[1], "'", 1)
856                         splt = string.split(splt[0], "/")
857                         if len(splt) < 3:
858                                 return False
859                         else:
860                                 
861                                 return splt[2]
862
863                 elif get_what == "prev":
864                         print "getting comics com prev: " + comicname
865                         splt = string.split(source, "Link_Previous: '", 1)
866                         splt = string.split(splt[1], "'", 1)
867                         splt = string.split(splt[0], "/")
868                         if len(splt) == 0:
869                                 return False
870                         else:
871                                 if len(splt[2]) == 10:
872                                         return splt[2]
873                                 else:
874                                         return False
875                 
876
877
878 # ------------UI
879
880 class ComicHomePlugin(hildondesktop.HomePluginItem):
881         # __gtype_name__ = 'ComicHomePlugin'
882         def __init__(self):
883                 hildondesktop.HomePluginItem.__init__(self)
884                 global supports_alpha
885                 self.set_settings(True)
886                 self.active_comics = self.get_active_comics()
887                 self.sizes = self.get_size_settings()
888                 self.new_width = self.sizes['width']
889                 self.new_height = self.sizes['height']
890                 self.connect("show-settings", self.show_options)
891                 self.osso_c = osso.Context("comic-widget", "0.3.0", False)
892                 self.osso_rpc = osso.Rpc(self.osso_c)
893                 self.keypointer = 0
894                 if len(self.active_comics) < 1:
895                         self.active_comics = ['xkcd']
896                 
897                 self.db = ComicDb(self.active_comics[self.keypointer])
898                 self.comicname = comics[self.active_comics[self.keypointer]]['name']
899                 self.set_name = "comicwidget"
900                 screen = self.get_screen()
901                 colormap = screen.get_rgba_colormap()
902                 if colormap == None:
903                         colormap = screen.get_rgb_colormap()
904                         supports_alpha = False
905                 else:
906                         supports_alpha = True
907                 self.set_colormap(colormap)
908                 self.set_app_paintable(True)
909                 #setup internal area
910                 self.set_size_request(self.sizes['width'], self.sizes['height'])
911                 self.imgvpos = 0;
912                 self.vbox = gtk.VBox()
913                 self.hbox = gtk.HBox()
914                 self.label = gtk.Label()
915                 self.label.set_alignment(xalign = 0, yalign = 0.5)
916                 self.label.set_use_markup(True)
917                 self.label.set_markup(self.get_markup())
918
919
920                 self.e_goweb = gtk.EventBox()
921                 self.e_goweb.set_name('goweb')
922                 self.e_goweb.set_size_request(self.sizes['width'] - 250, 48)
923                 # e_open is the eventbox for the image. Does nothing, but it's convenient to have it.
924                 # it will change to be a cairo thing when I have the time.
925                 self.e_open = gtk.EventBox()
926                 self.e_open.set_name('open')
927                 self.e_switch = gtk.EventBox()
928                 self.e_switch.set_name('switch')
929                 self.e_switch.set_size_request(48, 48)
930                 self.e_next = gtk.EventBox()
931                 self.e_next.set_name('next')
932                 self.e_next.set_size_request(48, 48)
933                 self.e_prev = gtk.EventBox()
934                 self.e_prev.set_name('prev')
935                 self.e_prev.set_size_request(48, 48)
936                 self.e_up = gtk.EventBox()
937                 self.e_up.set_name('up')
938                 self.e_up.set_size_request(48, 48)
939                 self.e_down = gtk.EventBox()
940                 self.e_down.set_name('down')
941                 self.e_down.set_size_request(48, 48)
942                 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
943                 self.comic_image.show()
944                 
945
946                 # Preload images into a dictionary, so that we can access them by the names of the eventboxes:
947
948                 self.images = {'up0':cairo.ImageSurface.create_from_png(imagedir + "up0-48x48.png"),
949                                         'up1':cairo.ImageSurface.create_from_png(imagedir + "up1-48x48.png"),
950                                         'down0':cairo.ImageSurface.create_from_png(imagedir + "down0-48x48.png"),
951                                         'down1':cairo.ImageSurface.create_from_png(imagedir + "down1-48x48.png"),
952                                         'next0':cairo.ImageSurface.create_from_png(imagedir + "next0-48x48.png"),
953                                         'next1':cairo.ImageSurface.create_from_png(imagedir + "next1-48x48.png"),
954                                         'prev0':cairo.ImageSurface.create_from_png(imagedir + "prev0-48x48.png"),
955                                         'prev1':cairo.ImageSurface.create_from_png(imagedir + "prev1-48x48.png"),
956                                         'switch0':cairo.ImageSurface.create_from_png(imagedir + "switch0-48x48.png"),
957                                         'switch1':cairo.ImageSurface.create_from_png(imagedir + "switch1-48x48.png"),
958                                         }
959
960
961
962                 self.e_goweb.add(self.label)
963                 self.e_goweb.set_events(gtk.gdk.BUTTON_RELEASE_MASK)
964                 self.e_goweb.connect("button-release-event", self.view_comic)
965
966                 self.e_open.add(self.comic_image)
967
968                 # self.e_switch.add(self.switch)
969                 self.e_switch.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
970                 self.e_switch.connect("button-press-event", self.button_press)
971                 self.e_switch.connect("button-release-event", self.button_release)
972                 self.e_switch.connect("leave-notify-event", self.button_release)
973
974
975                 # self.e_next.add(self.next)
976                 self.e_next.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
977                 self.e_next.connect("button-press-event", self.button_press)
978                 self.e_next.connect("button-release-event", self.button_release)
979                 self.e_next.connect("leave-notify-event", self.button_release)
980
981                 #self.e_prev.add(self.prev)
982                 self.e_prev.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
983                 self.e_prev.connect("button-press-event", self.button_press)
984                 self.e_prev.connect("button-release-event", self.button_release)
985                 self.e_prev.connect("leave-notify-event", self.button_release)
986
987                 #self.e_up.add(self.up)
988                 self.e_up.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
989                 self.e_up.connect("button-press-event", self.button_press)
990                 self.e_up.connect("button-release-event", self.button_release)
991                 self.e_up.connect("leave-notify-event", self.button_release)
992                 
993
994                 #self.e_down.add(self.down)
995                 self.e_down.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.LEAVE_NOTIFY)
996                 self.e_down.connect("button-press-event", self.button_press)
997                 self.e_down.connect("button-release-event", self.button_release)
998                 self.e_down.connect("leave-notify-event", self.button_release)
999
1000                 self.vbox.pack_start(self.e_open,False,False,0)
1001                 self.vbox.pack_end(self.hbox,False,False,0)
1002
1003                 self.hbox.pack_start(self.e_switch,False,False,0)
1004                 self.hbox.pack_start(self.e_goweb,False,False,5)
1005                 self.hbox.pack_end(self.e_next,False,False,0)
1006                 self.hbox.pack_end(self.e_prev,False,False,0)
1007                 self.hbox.pack_end(self.e_down,False,False,0)
1008                 self.hbox.pack_end(self.e_up,False,False,0)
1009                 self.vbox.show_all()
1010                 self.add(self.vbox)
1011
1012
1013
1014         # **************** Cairo draw functions ***************
1015
1016         def realize(self, widget):
1017                 global supports_alpha
1018
1019                 # To check if the display supports alpha channels, get the colormap
1020                 screen = self.get_screen()
1021                 colormap = screen.get_rgba_colormap()
1022                 if colormap == None:
1023                         print 'Your screen does not support alpha channels!'
1024                         colormap = screen.get_rgb_colormap()
1025                         supports_alpha = False
1026                 else:
1027                 #      print 'Your screen supports alpha channels!'
1028                         supports_alpha = True
1029
1030                 # Now we have a colormap appropriate for the screen, use it
1031                 self.set_colormap(colormap)
1032
1033                 return False
1034
1035         def do_expose_event(widget, event):
1036                 cr = widget.window.cairo_create()
1037                 widget.draw(widget.e_up, 0)
1038                 widget.draw(widget.e_down, 0)
1039                 widget.draw(widget.e_prev,0)
1040                 widget.draw(widget.e_next, 0)
1041                 widget.draw(widget.e_switch, 0)
1042 #               widget.writeit(widget.e_goweb, "asdf")
1043                 cr.set_operator(cairo.OPERATOR_OVER)
1044                 region = gtk.gdk.region_rectangle(event.area)
1045                 cr.region(region)
1046                 bg_color=gtk.gdk.color_parse('#000000')
1047                 if supports_alpha:
1048                         cr.set_source_rgba (0.0, 0.0, 0.0, 0.5)
1049                 else:
1050                         cr.set_source_rgb (0.0, 0.0, 0.0)
1051                         
1052                 cr.fill_preserve()
1053                 return False
1054
1055         # Function that draws a button preloaded in the images dictionary.
1056         def draw(self, target, postfix):
1057                 cr_e = target.window.cairo_create()
1058                 cr_e.set_source_rgba(0.0, 0.0, 0.0, 0.5) # Transparent
1059                 cr_e.set_operator(cairo.OPERATOR_SOURCE)
1060                 cr_e.paint()
1061                 cr_e.set_operator(cairo.OPERATOR_OVER)
1062                 cr_e.set_source_surface(self.images[target.get_name() + str(postfix)], 0, 0)
1063                 cr_e.paint()
1064
1065         # ********** Event handlers *************
1066
1067
1068         # Most events happen on release, so I moved all of them into the same event handler.
1069         def button_release(self, widget, event):
1070                 if event.type == gtk.gdk.BUTTON_RELEASE:
1071                         func = widget.get_name()
1072                         print "pressed: " + func
1073                         if func == 'up':
1074                                 self.e_open.remove(self.comic_image)
1075                                 self.imgvpos = self.imgvpos - ((self.sizes['height'] - 48) / 2.1)
1076                                 if self.imgvpos < 0:
1077                                         self.imgvpos = 0
1078                                 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1079                                 self.e_open.add(self.comic_image)
1080                                 self.e_open.show_all()
1081
1082                         elif func == 'down':
1083                                 self.e_open.remove(self.comic_image)
1084                                 self.imgvpos = self.imgvpos + ((self.sizes['height'] - 48) / 2.1)
1085                                 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1086                                 self.e_open.add(self.comic_image)
1087                                 self.e_open.show_all()
1088                         
1089                         elif func == 'next':
1090                                 self.db.currentcomic -= 1
1091                                 self.imgvpos = 0
1092                                 self.e_open.remove(self.comic_image)
1093                                 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1094                                 self.e_open.add(self.comic_image)
1095                                 self.e_open.show_all()
1096                                 self.e_goweb.remove(self.label)
1097                                 titl = str(self.db.db[self.db.currentcomic]['title'])
1098                                 if len(titl) > 22:
1099                                         titl = titl[:19] + "..."
1100                                 self.label.set_markup(self.get_markup())
1101                                 self.e_goweb.add(self.label)
1102                                 self.e_goweb.show_all()
1103
1104                         elif func == 'prev':
1105                                 self.db.currentcomic += 1
1106                                 self.imgvpos = 0
1107                                 self.e_open.remove(self.comic_image)
1108                                 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1109                                 self.e_open.add(self.comic_image)
1110                                 self.e_open.show_all()
1111                                 self.e_goweb.remove(self.label)
1112                                 titl = str(self.db.db[self.db.currentcomic]['title'])
1113                                 if len(titl) > 22:
1114                                         titl = titl[:19] + "..."
1115                                 self.label.set_markup(self.get_markup())
1116                                 self.e_goweb.add(self.label)
1117                                 self.e_goweb.show_all()
1118
1119                         elif func == 'switch':
1120                                 self.active_comics = self.get_active_comics()
1121                                 print "closing log"
1122                                 #sys.stdout.close()
1123                                 #sys.stdout = sys.__stdout__
1124                                 #sys.stderr.close()
1125                                 #sys.stderr = sys.__stderr__
1126                                 print "active comics: " + str(self.active_comics)
1127                                 self.keypointer = (self.keypointer + 1) % len(self.active_comics)
1128                                 self.imgvpos = 0
1129                                 self.db = []
1130                                 print "switching to " + self.active_comics[self.keypointer]
1131                                 self.comicname = comics[self.active_comics[self.keypointer]]['name']
1132                                 self.db = ComicDb(self.active_comics[self.keypointer])
1133                                 self.e_open.remove(self.comic_image)
1134                                 self.comic_image = self.get_resized_pixmap(self.db.get_comic(), self.imgvpos)
1135                                 self.e_goweb.remove(self.label)
1136                                 titl = str(self.db.db[self.db.currentcomic]['title'])
1137                                 if len(titl) > 22:
1138                                         titl = titl[:19] + "..."
1139                                 self.label.set_markup(self.get_markup())
1140                                 self.e_goweb.add(self.label)
1141                                 self.e_goweb.show_all()
1142                                 self.e_open.add(self.comic_image)
1143                                 self.e_open.show_all()  
1144
1145                         else:
1146                                 return False                    
1147                 self.draw(widget, "0")
1148
1149         # this one only switches to pressed button image
1150         def button_press(self, widget, event):
1151                 if not event.type == gtk.gdk.BUTTON_PRESS:
1152                         return False
1153                 self.draw(widget, "1")
1154
1155         # this one cancels a button press when pointer is moved out from it after press.
1156         def button_out(self, widget, event):
1157                 if not event.type == gtk.gdk.BUTTON_PRESS:
1158                         return False
1159                 self.draw(widget, "0")
1160
1161         # no fancy cairo stuff here. simply launches url in browser.
1162         def view_comic(self, widget, event):
1163                 if not event.type == gtk.gdk.BUTTON_RELEASE:
1164                         return False
1165                 link = self.db.get_link()
1166                 self.osso_rpc.rpc_run_with_defaults("osso_browser", "open_new_window", (link,))
1167
1168
1169
1170         # this function modifies the image to fit the widget and then returns it.
1171
1172         def get_resized_pixmap(self, filename, vpos):
1173                 print "vpos: " + str(vpos)
1174                 
1175                 width = int(self.sizes['width'])
1176                 height = int(self.sizes['height'])
1177                 picwidth = width - 2
1178                 maxheight = height - 48
1179
1180                 pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
1181                 parent_buf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, pixbuf.get_has_alpha(), 8, width, maxheight)
1182                 parent_buf.fill(0xffffffff)
1183                 retimg = gtk.Image()
1184                 # get correct width/height
1185                 new_height = 1
1186                 new_width = picwidth
1187                 new_height = int((float(picwidth) / pixbuf.get_width()) * pixbuf.get_height())
1188                 scaled_buf = pixbuf.scale_simple(new_width,new_height,gtk.gdk.INTERP_BILINEAR)
1189                 print "scaled height: " + str(scaled_buf.get_height())
1190                 if scaled_buf.get_height() > maxheight:
1191                         if scaled_buf.get_height() - (vpos + maxheight) < 0:
1192                                 vpos = scaled_buf.get_height() - maxheight
1193                         scaled_buf.copy_area(0,vpos,picwidth,maxheight,parent_buf,1,0)
1194                         # parent_buf.composite(scaled_buf, 0, 0, 478, maxheight, 0, 0, 478, maxheight, gtk.gdk.INTERP_BILINEAR, 0)
1195                         self.imgvpos = vpos
1196                 else:
1197                         tmpy = maxheight - scaled_buf.get_height()
1198                         if tmpy > 0:
1199                                 tmpy = tmpy / 2
1200
1201                         scaled_buf.copy_area(0,0,picwidth,scaled_buf.get_height(),parent_buf,1,tmpy)
1202                         self.imgvpos = 0
1203 #               test = parent_buf.render_pixmap_and_mask(0)
1204 #               parent_buf.add_alpha(True,chr(255),chr(255),chr(255))
1205                 retimg.set_from_pixbuf(parent_buf)
1206                 print retimg.get_pixel_size()
1207                 print "vpos after fixing image: " + str(self.imgvpos)
1208                 return retimg
1209
1210         def screen_changed(self, widget, old_screen=None):
1211                 global supports_alpha
1212
1213                 # To check if the display supports alpha channels, get the colormap
1214                 screen = self.get_screen()
1215                 colormap = screen.get_rgba_colormap()
1216                 if colormap == None:
1217                         print 'Your screen does not support alpha channels!'
1218                         colormap = screen.get_rgb_colormap()
1219                         supports_alpha = False
1220                 else:
1221                 #      print 'Your screen supports alpha channels!'
1222                         supports_alpha = True
1223
1224                 # Now we have a colormap appropriate for the screen, use it
1225                 self.set_colormap(colormap)
1226
1227                 return False
1228
1229
1230         def get_active_comics(self):
1231                 print "get list"
1232
1233                 if os.path.isfile(activecomics) == True:
1234                         print "found " + activecomics
1235                         dbf = open(activecomics, 'r')
1236                         dbr = csv.reader(dbf)
1237                         try:
1238                                 active = dbr.next()
1239                         except:
1240                                 active = []
1241                         dbf.close()
1242                         return active
1243                 # if not, copy it.
1244                 else:
1245                         print "file " + activecomics + " not found"
1246                         if not os.path.exists(dbdir):
1247                                 print "path not found: " + dbdir
1248                                 try:
1249                                         os.makedirs(dbdir)
1250                                 except:
1251                                         print "settings dir creation failed. Exiting."
1252                                         exit()
1253                         try:
1254 #                               dbf = file(activecomics, 'w')
1255 #                               dbf.close()
1256                                 dbf = open(activecomics, 'w')
1257                         except:
1258                                 print "error storing settings"
1259                                 exit()
1260                         dbw = csv.writer(dbf)
1261                         dbw.writerow(defaultcomics)
1262                         dbf.close()
1263                         ret = defaultcomics
1264                         return ret
1265
1266                 
1267         def get_markup(self):
1268                 if len(self.comicname + " " + str(self.db.db[self.db.currentcomic]['id'])) < 20:
1269                         return '<span size="12000" face="arial"> <b>' + self.comicname + '</b> ' + str(self.db.db[self.db.currentcomic]['id']) + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1270                 else:
1271                         return '<span size="12000" face="arial"> <b>' + self.comicname + '</b>' + '\n ' + str(self.db.db[self.db.currentcomic]['title']) + '</span>'
1272
1273                 
1274                 #check if settings file exists
1275
1276                 #if not, write defaults
1277
1278 # ************************* OPTION DIALOGS ********************************
1279
1280         def show_options(self, widget):
1281                 print "loading options dialog"
1282                 dialog = gtk.Dialog("Comic Widget", None, gtk.DIALOG_DESTROY_WITH_PARENT)
1283
1284                 about_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1285                 about_button.set_text("About", "See Author, Copyright and License information")
1286                 about_button.set_alignment(0,0,0,0)
1287                 about_button.connect("clicked", self.show_about)
1288
1289                 comics_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1290                 comics_button.set_text("Comics", "Choose which comics to view")
1291                 comics_button.set_alignment(0,0,0,0)
1292                 comics_button.connect("clicked", self.show_comics)
1293
1294                 # size_button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
1295                 # size_button.set_text("Change size", "Change the size of the widget")
1296                 # size_button.set_alignment(0,0,0,0)
1297                 # size_button.connect("clicked", self.show_size)
1298
1299                 print "adding buttons to dialog, starting with comics"
1300                 dialog.vbox.pack_start(comics_button, True, True, 0)
1301                 print "adding about button"
1302                 dialog.vbox.pack_start(about_button, True, True, 0)
1303                 # dialog.vbox.pack_start(size_button, True, True, 0)
1304                 print "show!"
1305                 dialog.show_all()
1306                 dialog.run()
1307                 dialog.destroy()
1308
1309         def show_about(self, widget):
1310                 print "in about dialog"
1311                 dialog = gtk.AboutDialog()
1312                 dialog.set_title("About")
1313                 dialog.set_name("Comic widget")
1314                 dialog.set_version(APP_VERSION)
1315                 dialog.set_copyright("Copyright 2010 Marcus Wikstrom")
1316                 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."])
1317                 dialog.set_logo(gtk.gdk.pixbuf_new_from_file("/opt/comic-widget/images/icon2-64x64.png"))
1318                 dialog.set_comments("Silly rabbit, tricks are for kids.")
1319                 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/>.""")
1320                 dialog.set_wrap_license(True)
1321                 dialog.show_all()
1322                 dialog.run()
1323                 dialog.destroy()
1324
1325         def show_comics(self, widget, data = None):
1326                 print "in comics dialog! yay!"
1327                 dialog = gtk.Dialog("Choose comics", None, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_NO_SEPARATOR)
1328                 comiclist = comics.keys()
1329                 comiclist.sort()
1330                 buttonlist = {}
1331                 self.connlist = {}
1332                 self.liststore = gtk.ListStore(str,str,bool)
1333                 for comicid in comiclist:
1334                         active = False
1335                         for acomic in self.active_comics:
1336                                 if acomic == comicid:
1337                                         active = True
1338                                         break
1339                         if active:
1340                                 self.liststore.append(["* " + comics[comicid]['name'],comicid, active])
1341                         else:
1342                                 self.liststore.append(["  " + comics[comicid]['name'],comicid, active])
1343                 self.treeview = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT)
1344                 self.treeview.set_model(self.liststore)
1345                 self.treeview.set_reorderable(True)
1346                 tvcolumn = gtk.TreeViewColumn('Column 0')
1347                 self.treeview.append_column(tvcolumn)
1348                 cell = gtk.CellRendererText()
1349                 tvcolumn.pack_start(cell, True)
1350                 tvcolumn.add_attribute(cell, 'text', 0)
1351
1352                 ts = hildon.PannableArea()
1353         
1354                 ts.set_size_request(-1, 280)
1355                 ts.add(self.treeview)
1356                 selection = self.treeview.get_selection()
1357
1358                 dialog.vbox.pack_start(ts,True,True,0)
1359                 dialog.show_all()
1360                 selection.connect("changed", self.pick_comic, comiclist)
1361                 dialog.run()
1362                 dialog.destroy()
1363
1364         def pick_comic(self, widget, data):
1365                 selected = widget.get_selected()
1366                 if selected == None:
1367                         return False
1368                 
1369                 print selected[0].get_value(selected[1],0) + ", " + selected[0].get_value(selected[1],1) + ", " + str(selected[0].get_value(selected[1],2))
1370                 if selected[0].get_value(selected[1],2):
1371                         self.remove_comic(selected[0].get_value(selected[1],1))
1372                         splt = selected[0].get_value(selected[1],0)[1:]
1373                         selected[0].set_value(selected[1],0," " + splt)
1374                         selected[0].set_value(selected[1],2,False)
1375                 else:
1376                         self.add_comic(selected[0].get_value(selected[1],1))
1377                         splt = selected[0].get_value(selected[1],0)[1:]
1378                         selected[0].set_value(selected[1],0,"*" + splt)
1379                         selected[0].set_value(selected[1],2,True)
1380
1381                                 
1382         def add_comic(self, comicid):
1383                 if os.path.isfile(activecomics) == True:
1384                         print "added " + comicid + " to " + str(self.active_comics)
1385                         try:
1386                                 found = self.active_comics.index(comicid)
1387                         except:
1388                                 self.active_comics.append(comicid)
1389                         dbf = open(activecomics, 'w')
1390                         dbw = csv.writer(dbf)
1391                         dbw.writerow(self.active_comics)
1392                         dbf.close()
1393
1394         def remove_comic(self, comicid):
1395                 if os.path.isfile(activecomics) == True:
1396                         print "removing " + comicid + " from " + str(self.active_comics)
1397                         try:
1398                                 del self.active_comics[self.active_comics.index(comicid)]
1399                         except:
1400                                 pass
1401                         dbf = open(activecomics, 'w')
1402                         dbw = csv.writer(dbf)
1403                         dbw.writerow(self.active_comics)
1404                         dbf.close()
1405
1406
1407         def get_size_settings(self):
1408                 # defaults = {'width':480,'height':230}
1409                 # options: w 798 700 600 500 480 400
1410                 # options: h 420 360 310 270 240 200
1411                 print "checking to see if config file is available"
1412                 if os.path.isfile(sizefile) == True:
1413                         print "found " + sizefile
1414                         dbf = open(sizefile, 'r')
1415                         dbr = csv.reader(dbf)
1416                         try:
1417                                 cfg = dbr.next()
1418                         except:
1419                                 dbf.close()
1420                                 print "empty config file"
1421                                 return defaults
1422                         dbf.close()
1423                         
1424                         sizes = {'width':int(cfg[0]),'height':int(cfg[1])}
1425                         if sizes['width'] > 798:
1426                                 sizes['width'] = 798
1427                         if sizes['width'] < 400:
1428                                 sizes['width'] = 400
1429                         if sizes['height'] > 420:
1430                                 sizes['height'] = 420
1431                         if sizes['height'] < 200:
1432                                 sizes['height'] = 200
1433                         
1434                         return sizes
1435                         
1436                         
1437                 # if not, copy it.
1438                 else:
1439                         print "no config file"
1440                         return defaults
1441                 
1442 hd_plugin_type = ComicHomePlugin
1443
1444
1445 if __name__ == "__main__":
1446         import gobject
1447         gobject.type_register(hd_plugin_type)
1448         obj = gobject.new(hd_plugin_type, plugin_id="plugin_id")
1449         obj.show_all()
1450         gtk.main()