Annotation of zogiLib/zogiLib.py, revision 1.68

1.59      casties     1: from AccessControl import ClassSecurityInfo
                      2: from Globals import package_home
                      3: from OFS.Folder import Folder
                      4: from OFS.Image import Image
                      5: from OFS.Image import File
1.56      dwinter     6: from OFS.SimpleItem import SimpleItem
1.59      casties     7: from Products.PageTemplates.PageTemplate import PageTemplate
1.1       dwinter     8: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
                      9: from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
1.59      casties    10: from types import *
                     11: from Globals import package_home, ImageFile
1.32      dwinter    12: from xml_helpers import getUniqueElementText,getText
1.59      casties    13: import cgi
1.1       dwinter    14: import os
1.59      casties    15: import random
1.1       dwinter    16: import re
                     17: import string
                     18: import urllib
1.59      casties    19: import xml.dom.minidom
1.1       dwinter    20: 
1.67      casties    21: ZOGIVERSION = "0.10.4b ROC 13.11.2006"
1.30      casties    22: 
                     23: def cropf(f):
                     24:     """returns a float with reduced precision"""
                     25:     return float(int(f * 10000)/10000.0)
                     26: 
1.28      casties    27: 
1.26      casties    28: def browserCheck(self):
1.18      casties    29:     """check the browsers request to find out the browser type"""
1.26      casties    30:     bt = {}
                     31:     ua = self.REQUEST.get_header("HTTP_USER_AGENT")
                     32:     bt['ua'] = ua
1.51      casties    33:     bt['isIE'] = False
                     34:     bt['isN4'] = False
1.50      casties    35:     if string.find(ua, 'MSIE') > -1:
                     36:         bt['isIE'] = True
                     37:     else:
                     38:         bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1)
                     39:         
                     40:     try:
                     41:         nav = ua[string.find(ua, '('):]
                     42:         ie = string.split(nav, "; ")[1]
                     43:         if string.find(ie, "MSIE") > -1:
                     44:             bt['versIE'] = string.split(ie, " ")[1]
                     45:     except: pass
                     46:     
1.26      casties    47:     bt['isMac'] = string.find(ua, 'Macintosh') > -1
                     48:     bt['isWin'] = string.find(ua, 'Windows') > -1
                     49:     bt['isIEWin'] = bt['isIE'] and bt['isWin']
                     50:     bt['isIEMac'] = bt['isIE'] and bt['isMac']
                     51:     bt['staticHTML'] = False
1.5       dwinter    52: 
1.26      casties    53:     return bt
1.5       dwinter    54: 
1.1       dwinter    55:     
1.56      dwinter    56: class zogiImage(SimpleItem):
1.4       dwinter    57:     """einzelnes Image"""
                     58:     meta_type="zogiImage"
                     59: 
1.66      casties    60:     manage_options=(
1.18      casties    61:         {'label':'Main config','action':'changeZogiImageForm'},
1.66      casties    62:        )+SimpleItem.manage_options
1.4       dwinter    63:     
                     64:     
                     65:     def __init__(self,id,title,baseUrl,queryString,content_type='',precondition=''):
                     66:         """init"""
                     67:         self.id=id
                     68:         self.title=title
1.46      casties    69:         if baseUrl:
                     70:             self.baseUrl=baseUrl
                     71:         else:
                     72:             self.baseUrl="http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler?"
                     73:             
1.4       dwinter    74:         self.queryString=queryString
                     75:         self.content_type=content_type
                     76:         self.precondition=precondition
                     77: 
1.56      dwinter    78:     #def getData(self):
                     79:     #    """getUrlData"""
                     80:     #    return urllib.urlopen(self.baseUrl+self.queryString)
1.4       dwinter    81: 
1.66      casties    82:     changeZogiImageForm = PageTemplateFile('zpt/changeZogiImageForm.zpt', globals())
1.4       dwinter    83:     
1.46      casties    84:     def index_html(self, REQUEST, RESPONSE):
                     85:         """service the request by redirecting to digilib server"""
1.66      casties    86:         # use status 307 = moved temporarily
                     87:         RESPONSE.redirect(self.baseUrl+self.queryString, status=307)
1.46      casties    88:         return ''
1.4       dwinter    89:         
1.66      casties    90: 
1.57      dwinter    91:     def rescale(self,width=None,height=None):
                     92:         """andere parameter im querystring"""
                     93:         qs=cgi.parse_qs(self.queryString)
                     94:         for x in qs.keys():
                     95:             if type(qs[x]) is ListType:
                     96:                 qs[x]=qs[x][0]
                     97:         
                     98:         if width:
                     99:             qs['dw']=width
                    100:         if height:
                    101:             qs['dh']=height
                    102:         
                    103:         qsneu=urllib.urlencode(qs)
                    104:         self.queryString=qsneu
                    105:         return "done"
                    106:     
1.66      casties   107:     
                    108:     def setWithDigilibURL(self,digilibUrl):
                    109:         """take all parameters from digilib URL string"""
                    110:         base = re.match('(.*?/Scaler\?)(.*)', digilibUrl)
                    111:         if base is not None:
                    112:             params = base.group(2).split('&')
                    113:             newparams = []
                    114:             # filter out the parameters we want
                    115:             for p in params:
                    116:                 (key, val) = p.split('=')
                    117:                 if key in ['fn','pn','dw','dh','ww','wh','wx','wy','mo']:
                    118:                     newparams.append(p)
                    119:             # set the new parameters
                    120:             if len(newparams) > 1:
                    121:                 self.baseUrl = base.group(1)
                    122:                 self.queryString = '&'.join(newparams)
                    123:                 return True
                    124:         
                    125:         return False
                    126:     
                    127:     
                    128:     def changeZogiImage(self,title,baseUrl, queryString, digilibUrl=None, RESPONSE=None):
                    129:         """change it"""
                    130:         self.title=title
                    131:         self.baseUrl=baseUrl
                    132:         self.queryString=queryString
                    133:         if digilibUrl is not None and len(digilibUrl) > 0:
                    134:             self.setWithDigilibURL(digilibUrl)
                    135:             
                    136:         if RESPONSE is not None:
                    137:             RESPONSE.redirect('manage_main')
                    138: 
                    139:     
1.4       dwinter   140: def manage_addZogiImageForm(self):
                    141:     """Form for adding"""
1.18      casties   142:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiImage.zpt')).__of__(self)
1.4       dwinter   143:     return pt()
                    144: 
                    145: 
1.66      casties   146: def manage_addZogiImage(self,id,title,baseUrl,queryString,digilibUrl=None,RESPONSE=None):
1.46      casties   147:     """add zogiimage"""
1.4       dwinter   148:     newObj=zogiImage(id,title,baseUrl, queryString)
                    149:     self.Destination()._setObject(id,newObj)
1.66      casties   150:     if digilibUrl is not None and len(digilibUrl) > 0:
                    151:         newObj.setWithDigilibURL(digilibUrl)
1.4       dwinter   152:     if RESPONSE is not None:
                    153:         RESPONSE.redirect('manage_main')
                    154: 
                    155: 
                    156: 
1.1       dwinter   157: class zogiLib(Folder):
1.50      casties   158:     """digilib frontend with ZOPE"""
1.1       dwinter   159: 
                    160:     meta_type="zogiLib"
1.32      dwinter   161:     #xxxx
1.54      dwinter   162:     security=ClassSecurityInfo()
                    163:     
1.18      casties   164:     manage_options = Folder.manage_options+(
                    165:             {'label':'Main Config','action':'changeZogiLibForm'},
                    166:             )
1.1       dwinter   167: 
1.37      casties   168:     def __init__(self, id, title, dlServerURL, layout="book", basePath="", dlTarget=None, dlToolbarBaseURL=None):
1.1       dwinter   169:         """init"""
                    170: 
1.63      casties   171:         self.id = id
                    172:         self.title = title
1.34      casties   173:         self.dlServerURL = dlServerURL
1.63      casties   174:         self.basePath = basePath
                    175:         self.layout = layout
1.44      casties   176:         self.dlTarget = dlTarget
1.1       dwinter   177: 
1.37      casties   178:         if dlToolbarBaseURL:
                    179:             self.dlToolbarBaseURL = dlToolbarBaseURL
                    180:         else:
                    181:             self.dlToolbarBaseURL = dlServerURL + "/digimage.jsp?"
1.59      casties   182:             
                    183:         self.manage_addFolder('template')
                    184: 
                    185: 
1.67      casties   186:     # form templates
1.59      casties   187:     main_book      = PageTemplateFile('zpt/main_book', globals())
                    188:     main_image     = PageTemplateFile('zpt/main_image', globals())
                    189:     main_metaData  = PageTemplateFile('zpt/main_metadata', globals())
1.61      casties   190:     #main_static    = PageTemplateFile('zpt/main_static', globals())
1.59      casties   191:     options        = PageTemplateFile('zpt/options', globals())
1.67      casties   192:     #changeForm     = PageTemplateFile('zpt/changeForm', globals())
1.59      casties   193: 
1.67      casties   194:     # display templates
1.61      casties   195:     aux_divs       = PageTemplateFile('zpt/aux_divs', globals())
                    196:     #aux_divsN4     = PageTemplateFile('zpt/aux_divsN4', globals())
1.59      casties   197:     img_div        = PageTemplateFile('zpt/img_div', globals())
                    198:     
1.67      casties   199:     # javascripts
1.59      casties   200:     head_js        = PageTemplateFile('zpt/head_js', globals())
                    201:     jslib_js       = PageTemplateFile('js/baselib.js', globals())
                    202:     dllib_js       = PageTemplateFile('js/dllib.js', globals())
                    203:     
1.67      casties   204:     # graphic files
1.59      casties   205:     arr_right = ImageFile('images/right.gif', globals())
                    206:     arr_left  = ImageFile('images/left.gif', globals())
                    207:     arr_up    = ImageFile('images/up.gif', globals())
1.61      casties   208:     arr_down  = ImageFile('images/down.gif', globals())
1.59      casties   209:     mark1     = ImageFile('images/mark1.gif', globals())
                    210:     mark2     = ImageFile('images/mark2.gif', globals())
                    211:     mark3     = ImageFile('images/mark3.gif', globals())
                    212:     mark4     = ImageFile('images/mark4.gif', globals())
                    213:     mark5     = ImageFile('images/mark5.gif', globals())
                    214:     mark6     = ImageFile('images/mark6.gif', globals())
                    215:     mark7     = ImageFile('images/mark7.gif', globals())
                    216:     mark8     = ImageFile('images/mark8.gif', globals())
                    217:     corner1   = ImageFile('images/olinks.gif', globals())
                    218:     corner2   = ImageFile('images/orechts.gif', globals())
                    219:     corner3   = ImageFile('images/ulinks.gif', globals())
                    220:     corner4   = ImageFile('images/urechts.gif', globals())    
                    221: 
1.37      casties   222: 
1.54      dwinter   223:     security.declareProtected('View','getLayout')
                    224:     def getLayout(self):
                    225:         """get Layout"""
                    226:         return self.layout
                    227:     
1.28      casties   228:     def version(self):
                    229:         """version information"""
                    230:         return ZOGIVERSION
                    231: 
1.32      dwinter   232:     def getContextStatic(self):
                    233:         """get all the contexts which go to static pages"""
                    234:         
1.33      dwinter   235:         try:
                    236:             dom=xml.dom.minidom.parse(urllib.urlopen(self.getMetaFileName()))
                    237:             contexts=dom.getElementsByTagName("context")
                    238: 
                    239:             ret=[]
                    240:             for context in contexts:
                    241:                 name=getUniqueElementText(context.getElementsByTagName("name"))
                    242: 
                    243:                 link=getUniqueElementText(context.getElementsByTagName("link"))
                    244:                 if name or link:
                    245:                     ret.append((name,link))
                    246:             return ret
                    247:         except:
                    248:             return []
1.32      dwinter   249: 
                    250:     def getContextDatabases(self):
                    251:         """get all dynamic contexts"""
1.33      dwinter   252:         try:
                    253:             dom=xml.dom.minidom.parse(urllib.urlopen(self.getMetaFileName()))
                    254:             contexts=dom.getElementsByTagName("context")
                    255:             ret=[]
                    256:             for context in contexts:
                    257:                 metaDataLinks=context.getElementsByTagName("meta-datalink")
                    258:                 for metaDataLink in metaDataLinks:
                    259:                     db=metaDataLink.getAttribute("db")
                    260:                     link=self.REQUEST['URL1']+"/dl_db?db=%s"%db
                    261:                     if db:
                    262:                         ret.append((db,link))
                    263:                 metaDataLinks=context.getElementsByTagName("meta-baselink")
                    264: 
                    265:                 for metaDataLink in metaDataLinks:
                    266:                     db=metaDataLink.getAttribute("db")
                    267:                     link=self.REQUEST['URL1']+"/dl_db?db=%s"%db
                    268:                     if db:
                    269:                         ret.append((db,link))
                    270: 
                    271:             return ret
                    272:         except:
1.45      casties   273: 
                    274:             return []
1.32      dwinter   275: 
1.39      casties   276: 
1.32      dwinter   277:     def formatHTML(self,url,label=None,viewUrl=None):
                    278: 
                    279:         sets=xml.dom.minidom.parse(urllib.urlopen(url)).getElementsByTagName('dataset')
                    280:         ret=""
1.59      casties   281:         #print label
1.32      dwinter   282:         if label:
                    283:             ret+="""<a href="%s">%s</a>"""%(viewUrl,label)
                    284:         for set in sets:
                    285:             ret+="<table>"
                    286:             for node in set.childNodes:
                    287:                 if hasattr(node,'tagName'):
                    288:                     tag=node.tagName
                    289:                     label=node.getAttribute("label")
                    290:                     if not label:
                    291:                         label=tag
                    292:                     text=getText(node.childNodes)
                    293:                     ret+="""<tr><td><b>%s:</b></td><td>%s</td></tr>"""%(label,text)
                    294:             ret+="</table>"
                    295:         return ret
1.39      casties   296: 
1.32      dwinter   297:     
                    298:     def getMetaData(self):
                    299:         """getMetaData"""
1.33      dwinter   300:         try:
                    301:             dom=xml.dom.minidom.parse(urllib.urlopen(self.getMetaFileName()))
                    302:         except:
                    303:             return "error metadata"
                    304:         
1.32      dwinter   305:         contexts=dom.getElementsByTagName("context")
                    306:         ret=[]
                    307:         db=self.getDLParam("db")
                    308:         ob=self.getDLParam("object")
                    309:         
                    310:         fn=self.getDLParam("fn")
                    311:         pn=self.getDLParam("pn")
                    312:         if not fn:
                    313:             fn=""
                    314:         if not pn:
                    315:             pn=""
                    316:         if not ob:
                    317:             ob=""
                    318:             
                    319:         for context in contexts:
                    320:             metaDataLinks=context.getElementsByTagName("meta-datalink")
                    321:             for metaDataLink in metaDataLinks:
                    322:                  
                    323:                 if (db==metaDataLink.getAttribute("db")) or (len(metaDataLinks)==1):
                    324:                     
                    325:                     link=getUniqueElementText(metaDataLink.getElementsByTagName("metadata-url"))
                    326:                     label=getUniqueElementText(metaDataLink.getElementsByTagName("label"))
                    327:                     url=getUniqueElementText(metaDataLink.getElementsByTagName("url"))
                    328: 
                    329:                     return self.formatHTML(link,label,url)
                    330: 
                    331:             metaDataLinks=context.getElementsByTagName("meta-baselink")
                    332:              
                    333:             for metaDataLink in metaDataLinks:
                    334:                 
                    335:                 if db==metaDataLink.getAttribute("db") or (len(metaDataLinks)==1):
                    336:                     
                    337:                     link=getUniqueElementText(metaDataLink.getElementsByTagName("metadata-url"))
                    338:                     label=getUniqueElementText(metaDataLink.getElementsByTagName("label"))
                    339:                     url=getUniqueElementText(metaDataLink.getElementsByTagName("url"))
                    340: 
                    341:                     return self.formatHTML(link+'fn=%s&pn=%s&object=%s'%(fn,pn,ob),label,url)
                    342:         return ret
                    343: 
1.39      casties   344: 
1.18      casties   345:     def getDLInfo(self):
                    346:         """get DLInfo from digilib server"""
                    347:         paramH={}
1.59      casties   348:         baseUrl=self.getDLBaseUrl()+"/dlInfo-xml.jsp"
1.61      casties   349:         #print "getdlinfo: ", baseUrl
1.18      casties   350:         try:
1.59      casties   351:             url=urllib.urlopen(baseUrl+'?'+self.getAllDLParams())
1.18      casties   352:             dom=xml.dom.minidom.parse(url)
                    353:             params=dom.getElementsByTagName('parameter')
                    354:             for param in params:
                    355:                 paramH[param.getAttribute('name')]=param.getAttribute('value')
                    356:             return paramH
                    357:         except:
1.34      casties   358:             return {}
1.18      casties   359: 
1.1       dwinter   360: 
1.59      casties   361:     def zogilibPath(self, otherbase=None):
                    362:         """returns an URL to the zogiLib instance"""
                    363:         url = self.REQUEST['URL1']
                    364:         # should end with "/"
                    365:         if len(url) > 0 and url[-1] != '/':
                    366:             url += '/'
                    367:         if type(otherbase) is str:
                    368:             url += otherbase
                    369:         else:
                    370:             url += self.basePath
1.61      casties   371:         # should still end with "/"
1.59      casties   372:         if len(url) > 0 and url[-1] != '/':
                    373:             url += '/'
                    374:         return url
1.19      casties   375: 
1.59      casties   376:     def zogilibAction(self, action, otherbase=None):
                    377:         """returns a URL with zogilib path and action"""
                    378:         url = self.zogilibPath(otherbase)
                    379:         url += action
                    380:         url += '?' + self.getAllDLParams();
                    381:         return url
                    382:     
                    383:     def getDLBaseUrl(self):
                    384:         """returns digilib base URL (sans servlet path)"""
                    385:         if self.dlServerURL[-1] == '?':
                    386:             # full Servlet URL -- remove last part
                    387:             si = self.dlServerURL.rindex('/servlet/')
                    388:             if si > 0:
                    389:                 return self.dlServerURL[:si]
                    390:             else:
                    391:                 # no servlet part :-(
                    392:                 return "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    393:         else:
                    394:             return self.dlServerURL 
1.19      casties   395:         
1.58      dwinter   396: 
1.59      casties   397:     def getScalerUrl(self,requestString=""):
1.58      dwinter   398:         """send scaler url"""
1.59      casties   399:         if self.dlServerURL[-1] == '?':
                    400:             # full Servlet URL
                    401:             return self.dlServerURL + requestString
                    402:         else:
1.58      dwinter   403:             return self.dlServerURL+'/servlet/Scaler?'+requestString
                    404:     
                    405:     def scaledImage(self,requestString=None):
                    406:         """scaled Image"""
                    407:         
                    408:         if not requestString:
                    409:             requestString=self.REQUEST['QUERY_STRING']
                    410:             
                    411:         self.REQUEST.RESPONSE.redirect(self.getScalerUrl(requestString))
                    412:         
                    413:         return True
                    414:     
                    415:         
1.65      casties   416:     def createScalerImg(self, requestString=None, bottom=0, side=0, width=500, height=500, options=None):
1.18      casties   417:         """generate Scaler IMG Tag"""
1.58      dwinter   418:         self.checkQuery()
1.63      casties   419:         bt = self.getBrowserType()
1.24      casties   420:         # override with parameters from session
                    421:         if  self.REQUEST.SESSION.has_key('scalerDiv'):
1.26      casties   422:             (requestString, bottom, side, width, height) = self.REQUEST.SESSION['scalerDiv']
1.24      casties   423:         # if not explicitly defined take normal request
1.18      casties   424:         if not requestString:
1.21      casties   425:             requestString = self.getAllDLParams()
1.59      casties   426:         url = self.getScalerUrl(requestString=requestString)
1.65      casties   427:         # take insets from options if present
                    428:         if options is not None:
                    429:             side = options.get('side', side)
                    430:             bottom = options.get('bottom', bottom)
1.24      casties   431:         # construct bottom and side insets
                    432:         b_par = ""
                    433:         s_par = ""
                    434:         if (bottom != 0) or (side != 0):
                    435:             b_par = "-" + str(int(bottom))
                    436:             s_par = "-" + str(int(side))
1.18      casties   437:         tag = ""
1.26      casties   438:         if bt['staticHTML']:
                    439:             tag += '<div id="scaler"><img id="pic" src="%s&dw=%i&dh=%i" /></div>'%(url, int(width-side), int(height-bottom))
1.18      casties   440:         else:
1.26      casties   441:             if bt['isN4']:
                    442:                 # N4 needs layers
                    443:                 tag += '<ilayer id="scaler">'
                    444:             else:
                    445:                 tag += '<div id="scaler">'
                    446:             tag += '<script type="text/javascript">'
                    447:             tag += "var ps = bestPicSize(getElement('scaler'));"
                    448:             # write img tag with javascript
                    449:             tag += 'document.write(\'<img id="pic" src="%s&dw=\'+(ps.width%s)+\'&dh=\'+(ps.height%s)+\'" />\');'%(url, s_par, b_par)
                    450:             tag += '</script>'
                    451:             if bt['isN4']:
                    452:                 tag += '</ilayer>'
                    453:             else:
                    454:                 tag += '</div>'
1.18      casties   455:         return tag
1.1       dwinter   456: 
1.26      casties   457:     def createScalerDiv(self, requestString = None, bottom = 0, side = 0, width=500, height=500):
1.23      casties   458:         """generate scaler img and table with navigation arrows"""
1.58      dwinter   459:         self.checkQuery()
1.24      casties   460:         if requestString != None or bottom != 0 or side != 0:
1.26      casties   461:             self.REQUEST.SESSION['scalerDiv'] = (requestString, bottom, side, width, height)
1.24      casties   462:         else:
                    463:             if self.REQUEST.SESSION.has_key('scalerDiv'):
1.26      casties   464:                 # make shure to remove unused parameter
1.24      casties   465:                 del self.REQUEST.SESSION['scalerDiv']
1.26      casties   466:                 
1.23      casties   467:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt/zogilib_img_div')).__of__(self)
                    468:         return pt()
                    469: 
1.1       dwinter   470:     def index_html(self):
                    471:         """main action"""
1.58      dwinter   472:         self.checkQuery()
1.59      casties   473:         tp = "main_template"
                    474:         tpt = self.layout
1.32      dwinter   475:         
1.60      casties   476:         if not hasattr(self, 'template'):
                    477:             # create template folder if it doesn't exist
1.61      casties   478:             print "no template folder -- creating"
1.60      casties   479:             self.manage_addFolder('template')
1.26      casties   480:             
1.60      casties   481:         pt = getattr(self.template, 'main_'+tpt)
1.18      casties   482:         return pt()
                    483: 
1.59      casties   484:     def checkQuery(self):
                    485:         """make shure that the query has been saved"""
                    486:         if not self.REQUEST.has_key('dlParams'):
                    487:             self.storeQuery()
1.1       dwinter   488: 
1.59      casties   489:     def storeQuery(self, more=None, withpt=False):
                    490:         """parse query parameters into a hash in REQUEST"""
                    491:         params = {}
1.1       dwinter   492:         for fm in self.REQUEST.form.keys():
1.59      casties   493:             params[fm] = self.REQUEST.form[fm]
1.21      casties   494:         # look for more
                    495:         if more:
                    496:             for fm in more.split('&'):
                    497:                 try:
                    498:                     pv = fm.split('=')
1.59      casties   499:                     params[pv[0]] = pv[1]
1.21      casties   500:                 except:
1.26      casties   501:                     pass
                    502:                 
1.21      casties   503:         # parse digilib mode parameter
1.59      casties   504:         if 'mo' in params:
                    505:             if len(params['mo']) > 0:
                    506:                 modes=params['mo'].split(',')
1.18      casties   507:         else:
1.59      casties   508:             modes = [];
                    509: 
                    510:         self.REQUEST.set('dlParams', params)
                    511:         self.REQUEST.set('dlModes', modes)
                    512:         
                    513:         # trigger get pt (from dlInfo) if requested
                    514:         if withpt:
                    515:             pt = self.getPT()
1.44      casties   516:             
1.59      casties   517:         return params
                    518:         
1.44      casties   519: 
1.53      casties   520:     def getDLParam(self, param, default=None):
                    521:         """returns parameter or default"""
1.59      casties   522:         self.checkQuery()
                    523:         dlParams = self.REQUEST.get('dlParams')
1.3       dwinter   524:         try:
1.59      casties   525:             return dlParams[param]
1.3       dwinter   526:         except:
1.53      casties   527:             return default
1.3       dwinter   528: 
1.18      casties   529:     def setDLParam(self, param, value):
                    530:         """sets parameter"""
1.59      casties   531:         self.checkQuery()
                    532:         dlParams = self.REQUEST.get('dlParams')
1.44      casties   533:         dlParams[param] = value
1.18      casties   534:         return
                    535: 
                    536:     def getAllDLParams(self):
                    537:         """parameter string for digilib"""
1.59      casties   538:         self.checkQuery()
                    539:         dlParams = self.REQUEST.get('dlParams')
1.18      casties   540:         # save modes
1.59      casties   541:         modes = self.REQUEST.get('dlModes')
                    542:         if modes:
                    543:             dlParams['mo'] = string.join(modes, ',')
1.18      casties   544:         # assemble query string
                    545:         ret = ""
                    546:         for param in dlParams.keys():
1.29      casties   547:             if dlParams[param] is None: continue
1.18      casties   548:             val = str(dlParams[param])
                    549:             if val != "":
                    550:                 ret += param + "=" + val + "&"
1.28      casties   551: 
1.18      casties   552:         # omit trailing "&"
                    553:         return ret.rstrip('&')
                    554:         
                    555:     def setDLParams(self,pn=None,ws=None,rot=None,brgt=None,cont=None):
                    556:         """setze Parameter"""
                    557: 
1.23      casties   558:         self.setDLParam('brgt', brgt)
                    559:         self.setDLParam('cont', cont)
                    560:         self.setDLParam('ws', ws)
                    561:         self.setDLParam('rot', rot)
1.18      casties   562: 
                    563:         if pn:
1.21      casties   564:             # unmark
                    565:             self.setDLParam('mk', None)
1.18      casties   566:             self.setDLParam('pn', pn)
                    567:             
                    568:         return self.display()
                    569: 
1.63      casties   570:     def getBrowserType(self):
                    571:         """get browser type object"""
                    572:         if self.REQUEST.SESSION.has_key('browserType'):
                    573:             return self.REQUEST.SESSION['browserType']
                    574:         else:
                    575:             bt = browserCheck(self)
                    576:             self.REQUEST.SESSION.set('browserType', bt)                        
                    577:             return bt
                    578: 
1.18      casties   579: 
                    580:     def display(self):
                    581:         """(re)display page"""
                    582:         params = self.getAllDLParams()
1.44      casties   583:             
1.21      casties   584:         if self.basePath:
                    585:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL2']+'?'+params)
                    586:         else:
                    587:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'?'+params)
1.18      casties   588: 
1.32      dwinter   589:     def getMetaFileName(self):
1.34      casties   590:         url=self.dlServerURL+'/dlContext-xml.jsp?'+self.getAllDLParams()
                    591:         return urlbase
1.26      casties   592: 
1.34      casties   593:     def getToolbarPageURL(self):
                    594:         """returns a toolbar-enabled page URL"""
1.37      casties   595:         url=self.dlToolbarBaseURL+self.getAllDLParams()
1.34      casties   596:         return url
1.32      dwinter   597:     
1.30      casties   598:     def getDLTarget(self):
                    599:         """returns dlTarget"""
                    600:         self.checkQuery()
                    601:         s = self.dlTarget
1.61      casties   602:         if (s is None) or (s == ""):
                    603: #            s = ""
                    604:             s = 'dl'
                    605:             if self.getDLParam('fn'):
                    606:                 s += "_" + self.getDLParam('fn')
                    607:             if self.getDLParam('pn'):
                    608:                 s += "_" + self.getDLParam('pn')
                    609: 
1.30      casties   610:         return s
                    611: 
1.59      casties   612:     def getPN(self):
1.61      casties   613:         """pagenumber"""
1.59      casties   614:         pn = int(self.getDLParam('pn', 1))
                    615:         return pn
                    616:     
1.18      casties   617:     def getPT(self):
1.61      casties   618:         """number of total pages"""
1.59      casties   619:         pt = self.getDLParam('pt', None)
                    620:         if pt is None:
                    621:             # get pt from dlInfo
                    622:             if self.REQUEST.has_key('dlInfo'):
                    623:                 info = self.REQUEST.get('dlInfo')
                    624:             else:
                    625:                 info = self.getDLInfo()
                    626:                 self.REQUEST.set('dlInfo', info)
                    627:             pt = int(info.get('pt', 1))
                    628:             self.setDLParam('pt', pt)
                    629:         return int(pt)
1.18      casties   630:     
                    631:     def hasMode(self, mode):
                    632:         """returns if mode is in the diglib mo parameter"""
1.59      casties   633:         return (mode in self.REQUEST.get('dlModes'))
1.18      casties   634: 
                    635:     def hasNextPage(self):
                    636:         """returns if there is a next page"""
                    637:         pn = self.getPN()
                    638:         pt = self.getPT()
                    639:         return (pn < pt)
                    640:    
                    641:     def hasPrevPage(self):
                    642:         """returns if there is a previous page"""
                    643:         pn = self.getPN()
                    644:         return (pn > 1)
1.1       dwinter   645: 
1.22      casties   646:     def canMoveLeft(self):
                    647:         """returns if its possible to move left"""
                    648:         wx = float(self.getDLParam('wx') or 0)
                    649:         return (wx > 0)
                    650: 
                    651:     def canMoveRight(self):
                    652:         """returns if its possible to move right"""
                    653:         wx = float(self.getDLParam('wx') or 0)
                    654:         ww = float(self.getDLParam('ww') or 1)
                    655:         return (wx + ww < 1)
                    656: 
                    657:     def canMoveUp(self):
                    658:         """returns if its possible to move up"""
                    659:         wy = float(self.getDLParam('wy') or 0)
                    660:         return (wy > 0)
                    661: 
                    662:     def canMoveDown(self):
                    663:         """returns if its possible to move down"""
                    664:         wy = float(self.getDLParam('wy') or 0)
                    665:         wh = float(self.getDLParam('wh') or 1)
                    666:         return (wy + wh < 1)
                    667: 
1.26      casties   668: 
                    669:     def dl_StaticHTML(self):
                    670:         """set rendering to static HTML"""
                    671:         self.checkQuery()
1.63      casties   672:         self.getBrowserType()['staticHTML'] = True
1.26      casties   673:         return self.display()
                    674: 
                    675:     def dl_DynamicHTML(self):
                    676:         """set rendering to dynamic HTML"""
                    677:         self.checkQuery()
1.63      casties   678:         self.getBrowserType()['staticHTML'] = False
1.26      casties   679:         return self.display()
1.1       dwinter   680:         
1.18      casties   681:     def dl_HMirror(self):
                    682:         """mirror action"""
1.44      casties   683:         modes = self.getSubSession('dlModes')
1.18      casties   684:         if 'hmir' in modes:
                    685:             modes.remove('hmir')
                    686:         else:
                    687:             modes.append('hmir')
1.1       dwinter   688: 
1.18      casties   689:         return self.display()
                    690:        
                    691:     def dl_VMirror(self):
                    692:         """mirror action"""
1.44      casties   693:         modes = self.getSubSession('dlModes')
1.18      casties   694:         if 'vmir' in modes:
                    695:             modes.remove('vmir')
                    696:         else:
                    697:             modes.append('vmir')
1.1       dwinter   698: 
1.18      casties   699:         return self.display()
1.1       dwinter   700: 
1.22      casties   701:     def dl_Zoom(self, z):
                    702:         """general zoom action"""
                    703:         ww1 = float(self.getDLParam('ww') or 1)
                    704:         wh1 = float(self.getDLParam('wh') or 1)
                    705:         wx = float(self.getDLParam('wx') or 0)
                    706:         wy = float(self.getDLParam('wy') or 0)
                    707:         ww2 = ww1 * z
                    708:         wh2 = wh1 * z
                    709:         wx += (ww1 - ww2) / 2
                    710:         wy += (wh1 - wh2) / 2
                    711:         ww2 = max(min(ww2, 1), 0)
                    712:         wh2 = max(min(wh2, 1), 0)
                    713:         wx = max(min(wx, 1), 0)
                    714:         wy = max(min(wy, 1), 0)
1.30      casties   715:         self.setDLParam('ww', cropf(ww2))
                    716:         self.setDLParam('wh', cropf(wh2))
                    717:         self.setDLParam('wx', cropf(wx))
                    718:         self.setDLParam('wy', cropf(wy))
1.22      casties   719:         return self.display()
                    720:         
                    721:     def dl_ZoomIn(self):
                    722:         """zoom in action"""
                    723:         z = 0.7071
                    724:         return self.dl_Zoom(z)
                    725: 
                    726:     def dl_ZoomOut(self):
                    727:         """zoom out action"""
                    728:         z = 1.4142
                    729:         return self.dl_Zoom(z)
                    730: 
                    731:     def dl_Move(self, dx, dy):
                    732:         """general move action"""
                    733:         ww = float(self.getDLParam('ww') or 1)
                    734:         wh = float(self.getDLParam('wh') or 1)
                    735:         wx = float(self.getDLParam('wx') or 0)
                    736:         wy = float(self.getDLParam('wy') or 0)
                    737:         wx += dx * 0.5 * ww
                    738:         wy += dy * 0.5 * wh
                    739:         wx = max(min(wx, 1), 0)
                    740:         wy = max(min(wy, 1), 0)
1.30      casties   741:         self.setDLParam('wx', cropf(wx))
                    742:         self.setDLParam('wy', cropf(wy))
1.22      casties   743:         return self.display()
                    744:         
                    745:     def dl_MoveLeft(self):
                    746:         """move left action"""
                    747:         return self.dl_Move(-1, 0)
                    748:     
                    749:     def dl_MoveRight(self):
                    750:         """move left action"""
                    751:         return self.dl_Move(1, 0)
                    752:     
                    753:     def dl_MoveUp(self):
                    754:         """move left action"""
                    755:         return self.dl_Move(0, -1)
                    756:     
                    757:     def dl_MoveDown(self):
                    758:         """move left action"""
                    759:         return self.dl_Move(0, 1)
                    760:     
1.18      casties   761:     def dl_WholePage(self):
                    762:         """zoom out action"""
                    763:         self.setDLParam('ww', 1)
                    764:         self.setDLParam('wh', 1)
                    765:         self.setDLParam('wx', 0)
                    766:         self.setDLParam('wy', 0)
                    767:         return self.display()
                    768:         
                    769:     def dl_PrevPage(self):
                    770:         """next page action"""
                    771:         pn = self.getPN() - 1
                    772:         if pn < 1:
                    773:             pn = 1
                    774:         self.setDLParam('pn', pn)
                    775:         # unmark
                    776:         self.setDLParam('mk', None)
                    777:         return self.display()
                    778:         
                    779:     def dl_NextPage(self):
                    780:         """next page action"""
                    781:         pn = self.getPN() + 1
                    782:         pt = self.getPT()
                    783:         if pn > pt:
                    784:             pn = pt
                    785:         self.setDLParam('pn', pn)
                    786:         # unmark
                    787:         self.setDLParam('mk', None)
                    788:         return self.display()
                    789: 
                    790:     def dl_FirstPage(self):
                    791:         """first page action"""
                    792:         self.setDLParam('pn', 1)
                    793:         # unmark
                    794:         self.setDLParam('mk', None)
                    795:         return self.display()
                    796:     
                    797:     def dl_LastPage(self):
                    798:         """last page action"""
                    799:         self.setDLParam('pn', self.getPT())
                    800:         # unmark
                    801:         self.setDLParam('mk', None)
                    802:         return self.display()
                    803: 
1.32      dwinter   804:     def dl_db(self,db):
                    805:         """set db"""
                    806:         self.setDLParam('db',db)
                    807:         self.display()
1.1       dwinter   808: 
1.18      casties   809:     def changeZogiLibForm(self):
                    810:         """Main configuration"""
                    811:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/changeZogiLibForm.zpt')).__of__(self)
                    812:         return pt()
1.1       dwinter   813:     
1.37      casties   814:     def changeZogiLib(self,title,dlServerURL, version, basePath, dlTarget, dlToolbarBaseURL, RESPONSE=None):
1.18      casties   815:         """change it"""
                    816:         self.title=title
1.35      casties   817:         self.dlServerURL=dlServerURL
1.21      casties   818:         self.basePath = basePath
1.18      casties   819:         self.layout=version
1.44      casties   820:         self.dlTarget = dlTarget
1.3       dwinter   821: 
1.37      casties   822:         if dlToolbarBaseURL:
                    823:             self.dlToolbarBaseURL = dlToolbarBaseURL
                    824:         else:
                    825:             self.dlToolbarBaseURL = dlServerURL + "/digimage.jsp?"
                    826: 
1.18      casties   827:         if RESPONSE is not None:
                    828:             RESPONSE.redirect('manage_main')
1.8       dwinter   829: 
1.35      casties   830: 
                    831: 
                    832:     ##
1.44      casties   833:     ## odds and ends
1.35      casties   834:     ##
                    835: 
                    836:     def repairZogilib(self, obj=None):
                    837:         """change stuff that broke on upgrading"""
                    838: 
                    839:         msg = ""
                    840: 
                    841:         if not obj:
                    842:             obj = self.getPhysicalRoot()
                    843: 
                    844:         print "starting in ", obj
                    845:         
                    846:         entries=obj.ZopeFind(obj,obj_metatypes=['zogiLib'],search_sub=1)
                    847: 
                    848:         for entry in entries:
                    849:             print "  found ", entry
1.37      casties   850:             #
                    851:             # replace digilibBaseUrl by dlServerURL
1.35      casties   852:             if hasattr(entry[1], 'digilibBaseUrl'):
1.37      casties   853:                 msg += "  fixing digilibBaseUrl in "+entry[0]+"\n"
1.36      casties   854:                 entry[1].dlServerURL = re.sub('/servlet/Scaler\?','',entry[1].digilibBaseUrl)
1.35      casties   855:                 del entry[1].digilibBaseUrl
                    856:                 
1.37      casties   857:             #
                    858:             # add dlToolbarBaseURL
                    859:             if not hasattr(entry[1], 'dlToolbarBaseURL'):
                    860:                 msg += "  fixing dlToolbarBaseURL in "+entry[0]+"\n"
                    861:                 entry[1].dlToolbarBaseURL = entry[1].dlServerURL + "/digimage.jsp?"
                    862:                 
1.35      casties   863:         return msg+"\n\nfixed all zogilib instances in: "+obj.title
                    864: 
1.8       dwinter   865:           
1.1       dwinter   866: def manage_addZogiLibForm(self):
                    867:     """interface for adding zogilib"""
1.18      casties   868:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiLibForm')).__of__(self)
1.1       dwinter   869:     return pt()
                    870: 
1.44      casties   871: def manage_addZogiLib(self,id,title,dlServerURL,version="book",basePath="",dlTarget=None,dlToolbarBaseURL=None,RESPONSE=None):
1.1       dwinter   872:     """add dgilib"""
1.43      casties   873:     newObj=zogiLib(id,title,dlServerURL, version, basePath, dlTarget, dlToolbarBaseURL)
1.1       dwinter   874:     self.Destination()._setObject(id,newObj)
                    875:     if RESPONSE is not None:
                    876:         RESPONSE.redirect('manage_main')
1.29      casties   877: 
                    878: 
                    879: class zogiLibPageTemplate(ZopePageTemplate):
                    880:     """pageTemplate Objekt"""
                    881:     meta_type="zogiLib_pageTemplate"
                    882: 
                    883: 
                    884: ## def __init__(self, id, text=None, contentType=None):
                    885: ##         self.id = str(id)
                    886: ##         self.ZBindings_edit(self._default_bindings)
                    887: ##         if text is None:
                    888: ##             text = open(self._default_cont).read()
                    889: ##         self.pt_edit(text, contentType)
                    890: 
                    891: def manage_addZogiLibPageTemplateForm(self):
                    892:     """Form for adding"""
                    893:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiLibPageTemplateForm')).__of__(self)
                    894:     return pt()
                    895: 
1.63      casties   896: def manage_addZogiLibPageTemplate(self, title=None, layout=None, text=None,
1.29      casties   897:                            REQUEST=None, submit=None):
                    898:     "Add a Page Template with optional file content."
                    899: 
1.62      casties   900:     if not layout: layout = "book"
1.63      casties   901:     id = 'main_%s'%layout
1.29      casties   902:     self._setObject(id, zogiLibPageTemplate(id))
                    903:     ob = getattr(self, id)
1.68    ! casties   904:     ob.pt_edit(open(os.path.join(package_home(globals()),'zpt/main_%s.zpt'%layout)).read(),'text/html')
1.29      casties   905:     if title:
                    906:         ob.pt_setTitle(title)
                    907:     try:
1.63      casties   908:         url = self.DestinationURL()
1.29      casties   909:     except AttributeError:
1.63      casties   910:         url = REQUEST['URL1']
1.29      casties   911:         
1.63      casties   912:     url = "%s/%s" % (url, urllib.quote(id))
                    913:     REQUEST.RESPONSE.redirect(url+'/manage_main')
1.29      casties   914:     return ''
1.35      casties   915: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>