Annotation of zogiLib/zogiLib.py, revision 1.70

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.70    ! dwinter   361:     def zogilibPathOLD(self, otherbase=None):
1.59      casties   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.70    ! dwinter   376:     def zogilibPath(self, otherbase=None):
        !           377:         """returns an URL to the zogiLib instance"""
        !           378:         url = self.absolute_url()
        !           379:         # should end with "/"
        !           380:         if len(url) > 0 and url[-1] != '/':
        !           381:             url += '/'
        !           382:         if type(otherbase) is str:
        !           383:             url += otherbase
        !           384:         else:
        !           385:             url += self.basePath
        !           386:         # should still end with "/"
        !           387:         if len(url) > 0 and url[-1] != '/':
        !           388:             url += '/'
        !           389:         return url
1.59      casties   390:     def zogilibAction(self, action, otherbase=None):
                    391:         """returns a URL with zogilib path and action"""
                    392:         url = self.zogilibPath(otherbase)
                    393:         url += action
                    394:         url += '?' + self.getAllDLParams();
                    395:         return url
                    396:     
                    397:     def getDLBaseUrl(self):
                    398:         """returns digilib base URL (sans servlet path)"""
                    399:         if self.dlServerURL[-1] == '?':
                    400:             # full Servlet URL -- remove last part
                    401:             si = self.dlServerURL.rindex('/servlet/')
                    402:             if si > 0:
                    403:                 return self.dlServerURL[:si]
                    404:             else:
                    405:                 # no servlet part :-(
                    406:                 return "http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary"
                    407:         else:
                    408:             return self.dlServerURL 
1.19      casties   409:         
1.58      dwinter   410: 
1.59      casties   411:     def getScalerUrl(self,requestString=""):
1.58      dwinter   412:         """send scaler url"""
1.59      casties   413:         if self.dlServerURL[-1] == '?':
                    414:             # full Servlet URL
                    415:             return self.dlServerURL + requestString
                    416:         else:
1.58      dwinter   417:             return self.dlServerURL+'/servlet/Scaler?'+requestString
                    418:     
                    419:     def scaledImage(self,requestString=None):
                    420:         """scaled Image"""
                    421:         
                    422:         if not requestString:
                    423:             requestString=self.REQUEST['QUERY_STRING']
                    424:             
                    425:         self.REQUEST.RESPONSE.redirect(self.getScalerUrl(requestString))
                    426:         
                    427:         return True
                    428:     
                    429:         
1.65      casties   430:     def createScalerImg(self, requestString=None, bottom=0, side=0, width=500, height=500, options=None):
1.18      casties   431:         """generate Scaler IMG Tag"""
1.58      dwinter   432:         self.checkQuery()
1.63      casties   433:         bt = self.getBrowserType()
1.24      casties   434:         # override with parameters from session
                    435:         if  self.REQUEST.SESSION.has_key('scalerDiv'):
1.26      casties   436:             (requestString, bottom, side, width, height) = self.REQUEST.SESSION['scalerDiv']
1.24      casties   437:         # if not explicitly defined take normal request
1.18      casties   438:         if not requestString:
1.21      casties   439:             requestString = self.getAllDLParams()
1.59      casties   440:         url = self.getScalerUrl(requestString=requestString)
1.65      casties   441:         # take insets from options if present
                    442:         if options is not None:
                    443:             side = options.get('side', side)
                    444:             bottom = options.get('bottom', bottom)
1.24      casties   445:         # construct bottom and side insets
                    446:         b_par = ""
                    447:         s_par = ""
                    448:         if (bottom != 0) or (side != 0):
                    449:             b_par = "-" + str(int(bottom))
                    450:             s_par = "-" + str(int(side))
1.18      casties   451:         tag = ""
1.26      casties   452:         if bt['staticHTML']:
                    453:             tag += '<div id="scaler"><img id="pic" src="%s&dw=%i&dh=%i" /></div>'%(url, int(width-side), int(height-bottom))
1.18      casties   454:         else:
1.26      casties   455:             if bt['isN4']:
                    456:                 # N4 needs layers
                    457:                 tag += '<ilayer id="scaler">'
                    458:             else:
                    459:                 tag += '<div id="scaler">'
                    460:             tag += '<script type="text/javascript">'
                    461:             tag += "var ps = bestPicSize(getElement('scaler'));"
                    462:             # write img tag with javascript
                    463:             tag += 'document.write(\'<img id="pic" src="%s&dw=\'+(ps.width%s)+\'&dh=\'+(ps.height%s)+\'" />\');'%(url, s_par, b_par)
                    464:             tag += '</script>'
                    465:             if bt['isN4']:
                    466:                 tag += '</ilayer>'
                    467:             else:
                    468:                 tag += '</div>'
1.18      casties   469:         return tag
1.1       dwinter   470: 
1.26      casties   471:     def createScalerDiv(self, requestString = None, bottom = 0, side = 0, width=500, height=500):
1.23      casties   472:         """generate scaler img and table with navigation arrows"""
1.58      dwinter   473:         self.checkQuery()
1.24      casties   474:         if requestString != None or bottom != 0 or side != 0:
1.26      casties   475:             self.REQUEST.SESSION['scalerDiv'] = (requestString, bottom, side, width, height)
1.24      casties   476:         else:
                    477:             if self.REQUEST.SESSION.has_key('scalerDiv'):
1.26      casties   478:                 # make shure to remove unused parameter
1.24      casties   479:                 del self.REQUEST.SESSION['scalerDiv']
1.26      casties   480:                 
1.69      casties   481:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt/img_div')).__of__(self)
1.23      casties   482:         return pt()
                    483: 
1.1       dwinter   484:     def index_html(self):
                    485:         """main action"""
1.58      dwinter   486:         self.checkQuery()
1.59      casties   487:         tp = "main_template"
                    488:         tpt = self.layout
1.32      dwinter   489:         
1.60      casties   490:         if not hasattr(self, 'template'):
                    491:             # create template folder if it doesn't exist
1.61      casties   492:             print "no template folder -- creating"
1.60      casties   493:             self.manage_addFolder('template')
1.26      casties   494:             
1.60      casties   495:         pt = getattr(self.template, 'main_'+tpt)
1.18      casties   496:         return pt()
                    497: 
1.59      casties   498:     def checkQuery(self):
                    499:         """make shure that the query has been saved"""
                    500:         if not self.REQUEST.has_key('dlParams'):
                    501:             self.storeQuery()
1.1       dwinter   502: 
1.59      casties   503:     def storeQuery(self, more=None, withpt=False):
                    504:         """parse query parameters into a hash in REQUEST"""
                    505:         params = {}
1.1       dwinter   506:         for fm in self.REQUEST.form.keys():
1.59      casties   507:             params[fm] = self.REQUEST.form[fm]
1.21      casties   508:         # look for more
                    509:         if more:
                    510:             for fm in more.split('&'):
                    511:                 try:
                    512:                     pv = fm.split('=')
1.59      casties   513:                     params[pv[0]] = pv[1]
1.21      casties   514:                 except:
1.26      casties   515:                     pass
                    516:                 
1.21      casties   517:         # parse digilib mode parameter
1.59      casties   518:         if 'mo' in params:
                    519:             if len(params['mo']) > 0:
                    520:                 modes=params['mo'].split(',')
1.18      casties   521:         else:
1.59      casties   522:             modes = [];
                    523: 
                    524:         self.REQUEST.set('dlParams', params)
                    525:         self.REQUEST.set('dlModes', modes)
                    526:         
                    527:         # trigger get pt (from dlInfo) if requested
                    528:         if withpt:
                    529:             pt = self.getPT()
1.44      casties   530:             
1.59      casties   531:         return params
                    532:         
1.44      casties   533: 
1.53      casties   534:     def getDLParam(self, param, default=None):
                    535:         """returns parameter or default"""
1.59      casties   536:         self.checkQuery()
                    537:         dlParams = self.REQUEST.get('dlParams')
1.3       dwinter   538:         try:
1.59      casties   539:             return dlParams[param]
1.3       dwinter   540:         except:
1.53      casties   541:             return default
1.3       dwinter   542: 
1.18      casties   543:     def setDLParam(self, param, value):
                    544:         """sets parameter"""
1.59      casties   545:         self.checkQuery()
                    546:         dlParams = self.REQUEST.get('dlParams')
1.44      casties   547:         dlParams[param] = value
1.18      casties   548:         return
                    549: 
                    550:     def getAllDLParams(self):
                    551:         """parameter string for digilib"""
1.59      casties   552:         self.checkQuery()
                    553:         dlParams = self.REQUEST.get('dlParams')
1.18      casties   554:         # save modes
1.59      casties   555:         modes = self.REQUEST.get('dlModes')
                    556:         if modes:
                    557:             dlParams['mo'] = string.join(modes, ',')
1.18      casties   558:         # assemble query string
                    559:         ret = ""
                    560:         for param in dlParams.keys():
1.29      casties   561:             if dlParams[param] is None: continue
1.18      casties   562:             val = str(dlParams[param])
                    563:             if val != "":
                    564:                 ret += param + "=" + val + "&"
1.28      casties   565: 
1.18      casties   566:         # omit trailing "&"
                    567:         return ret.rstrip('&')
                    568:         
                    569:     def setDLParams(self,pn=None,ws=None,rot=None,brgt=None,cont=None):
                    570:         """setze Parameter"""
                    571: 
1.23      casties   572:         self.setDLParam('brgt', brgt)
                    573:         self.setDLParam('cont', cont)
                    574:         self.setDLParam('ws', ws)
                    575:         self.setDLParam('rot', rot)
1.18      casties   576: 
                    577:         if pn:
1.21      casties   578:             # unmark
                    579:             self.setDLParam('mk', None)
1.18      casties   580:             self.setDLParam('pn', pn)
                    581:             
                    582:         return self.display()
                    583: 
1.63      casties   584:     def getBrowserType(self):
                    585:         """get browser type object"""
                    586:         if self.REQUEST.SESSION.has_key('browserType'):
                    587:             return self.REQUEST.SESSION['browserType']
                    588:         else:
                    589:             bt = browserCheck(self)
                    590:             self.REQUEST.SESSION.set('browserType', bt)                        
                    591:             return bt
                    592: 
1.18      casties   593: 
                    594:     def display(self):
                    595:         """(re)display page"""
                    596:         params = self.getAllDLParams()
1.44      casties   597:             
1.21      casties   598:         if self.basePath:
                    599:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL2']+'?'+params)
                    600:         else:
                    601:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'?'+params)
1.18      casties   602: 
1.32      dwinter   603:     def getMetaFileName(self):
1.34      casties   604:         url=self.dlServerURL+'/dlContext-xml.jsp?'+self.getAllDLParams()
                    605:         return urlbase
1.26      casties   606: 
1.34      casties   607:     def getToolbarPageURL(self):
                    608:         """returns a toolbar-enabled page URL"""
1.37      casties   609:         url=self.dlToolbarBaseURL+self.getAllDLParams()
1.34      casties   610:         return url
1.32      dwinter   611:     
1.30      casties   612:     def getDLTarget(self):
                    613:         """returns dlTarget"""
                    614:         self.checkQuery()
                    615:         s = self.dlTarget
1.61      casties   616:         if (s is None) or (s == ""):
                    617: #            s = ""
                    618:             s = 'dl'
                    619:             if self.getDLParam('fn'):
                    620:                 s += "_" + self.getDLParam('fn')
                    621:             if self.getDLParam('pn'):
                    622:                 s += "_" + self.getDLParam('pn')
                    623: 
1.30      casties   624:         return s
                    625: 
1.59      casties   626:     def getPN(self):
1.61      casties   627:         """pagenumber"""
1.59      casties   628:         pn = int(self.getDLParam('pn', 1))
                    629:         return pn
                    630:     
1.18      casties   631:     def getPT(self):
1.61      casties   632:         """number of total pages"""
1.59      casties   633:         pt = self.getDLParam('pt', None)
                    634:         if pt is None:
                    635:             # get pt from dlInfo
                    636:             if self.REQUEST.has_key('dlInfo'):
                    637:                 info = self.REQUEST.get('dlInfo')
                    638:             else:
                    639:                 info = self.getDLInfo()
                    640:                 self.REQUEST.set('dlInfo', info)
                    641:             pt = int(info.get('pt', 1))
                    642:             self.setDLParam('pt', pt)
                    643:         return int(pt)
1.18      casties   644:     
                    645:     def hasMode(self, mode):
                    646:         """returns if mode is in the diglib mo parameter"""
1.59      casties   647:         return (mode in self.REQUEST.get('dlModes'))
1.18      casties   648: 
                    649:     def hasNextPage(self):
                    650:         """returns if there is a next page"""
                    651:         pn = self.getPN()
                    652:         pt = self.getPT()
                    653:         return (pn < pt)
                    654:    
                    655:     def hasPrevPage(self):
                    656:         """returns if there is a previous page"""
                    657:         pn = self.getPN()
                    658:         return (pn > 1)
1.1       dwinter   659: 
1.22      casties   660:     def canMoveLeft(self):
                    661:         """returns if its possible to move left"""
                    662:         wx = float(self.getDLParam('wx') or 0)
                    663:         return (wx > 0)
                    664: 
                    665:     def canMoveRight(self):
                    666:         """returns if its possible to move right"""
                    667:         wx = float(self.getDLParam('wx') or 0)
                    668:         ww = float(self.getDLParam('ww') or 1)
                    669:         return (wx + ww < 1)
                    670: 
                    671:     def canMoveUp(self):
                    672:         """returns if its possible to move up"""
                    673:         wy = float(self.getDLParam('wy') or 0)
                    674:         return (wy > 0)
                    675: 
                    676:     def canMoveDown(self):
                    677:         """returns if its possible to move down"""
                    678:         wy = float(self.getDLParam('wy') or 0)
                    679:         wh = float(self.getDLParam('wh') or 1)
                    680:         return (wy + wh < 1)
                    681: 
1.26      casties   682: 
                    683:     def dl_StaticHTML(self):
                    684:         """set rendering to static HTML"""
                    685:         self.checkQuery()
1.63      casties   686:         self.getBrowserType()['staticHTML'] = True
1.26      casties   687:         return self.display()
                    688: 
                    689:     def dl_DynamicHTML(self):
                    690:         """set rendering to dynamic HTML"""
                    691:         self.checkQuery()
1.63      casties   692:         self.getBrowserType()['staticHTML'] = False
1.26      casties   693:         return self.display()
1.1       dwinter   694:         
1.18      casties   695:     def dl_HMirror(self):
                    696:         """mirror action"""
1.44      casties   697:         modes = self.getSubSession('dlModes')
1.18      casties   698:         if 'hmir' in modes:
                    699:             modes.remove('hmir')
                    700:         else:
                    701:             modes.append('hmir')
1.1       dwinter   702: 
1.18      casties   703:         return self.display()
                    704:        
                    705:     def dl_VMirror(self):
                    706:         """mirror action"""
1.44      casties   707:         modes = self.getSubSession('dlModes')
1.18      casties   708:         if 'vmir' in modes:
                    709:             modes.remove('vmir')
                    710:         else:
                    711:             modes.append('vmir')
1.1       dwinter   712: 
1.18      casties   713:         return self.display()
1.1       dwinter   714: 
1.22      casties   715:     def dl_Zoom(self, z):
                    716:         """general zoom action"""
                    717:         ww1 = float(self.getDLParam('ww') or 1)
                    718:         wh1 = float(self.getDLParam('wh') or 1)
                    719:         wx = float(self.getDLParam('wx') or 0)
                    720:         wy = float(self.getDLParam('wy') or 0)
                    721:         ww2 = ww1 * z
                    722:         wh2 = wh1 * z
                    723:         wx += (ww1 - ww2) / 2
                    724:         wy += (wh1 - wh2) / 2
                    725:         ww2 = max(min(ww2, 1), 0)
                    726:         wh2 = max(min(wh2, 1), 0)
                    727:         wx = max(min(wx, 1), 0)
                    728:         wy = max(min(wy, 1), 0)
1.30      casties   729:         self.setDLParam('ww', cropf(ww2))
                    730:         self.setDLParam('wh', cropf(wh2))
                    731:         self.setDLParam('wx', cropf(wx))
                    732:         self.setDLParam('wy', cropf(wy))
1.22      casties   733:         return self.display()
                    734:         
                    735:     def dl_ZoomIn(self):
                    736:         """zoom in action"""
                    737:         z = 0.7071
                    738:         return self.dl_Zoom(z)
                    739: 
                    740:     def dl_ZoomOut(self):
                    741:         """zoom out action"""
                    742:         z = 1.4142
                    743:         return self.dl_Zoom(z)
                    744: 
                    745:     def dl_Move(self, dx, dy):
                    746:         """general move action"""
                    747:         ww = float(self.getDLParam('ww') or 1)
                    748:         wh = float(self.getDLParam('wh') or 1)
                    749:         wx = float(self.getDLParam('wx') or 0)
                    750:         wy = float(self.getDLParam('wy') or 0)
                    751:         wx += dx * 0.5 * ww
                    752:         wy += dy * 0.5 * wh
                    753:         wx = max(min(wx, 1), 0)
                    754:         wy = max(min(wy, 1), 0)
1.30      casties   755:         self.setDLParam('wx', cropf(wx))
                    756:         self.setDLParam('wy', cropf(wy))
1.22      casties   757:         return self.display()
                    758:         
                    759:     def dl_MoveLeft(self):
                    760:         """move left action"""
                    761:         return self.dl_Move(-1, 0)
                    762:     
                    763:     def dl_MoveRight(self):
                    764:         """move left action"""
                    765:         return self.dl_Move(1, 0)
                    766:     
                    767:     def dl_MoveUp(self):
                    768:         """move left action"""
                    769:         return self.dl_Move(0, -1)
                    770:     
                    771:     def dl_MoveDown(self):
                    772:         """move left action"""
                    773:         return self.dl_Move(0, 1)
                    774:     
1.18      casties   775:     def dl_WholePage(self):
                    776:         """zoom out action"""
                    777:         self.setDLParam('ww', 1)
                    778:         self.setDLParam('wh', 1)
                    779:         self.setDLParam('wx', 0)
                    780:         self.setDLParam('wy', 0)
                    781:         return self.display()
                    782:         
                    783:     def dl_PrevPage(self):
                    784:         """next page action"""
                    785:         pn = self.getPN() - 1
                    786:         if pn < 1:
                    787:             pn = 1
                    788:         self.setDLParam('pn', pn)
                    789:         # unmark
                    790:         self.setDLParam('mk', None)
                    791:         return self.display()
                    792:         
                    793:     def dl_NextPage(self):
                    794:         """next page action"""
                    795:         pn = self.getPN() + 1
                    796:         pt = self.getPT()
                    797:         if pn > pt:
                    798:             pn = pt
                    799:         self.setDLParam('pn', pn)
                    800:         # unmark
                    801:         self.setDLParam('mk', None)
                    802:         return self.display()
                    803: 
                    804:     def dl_FirstPage(self):
                    805:         """first page action"""
                    806:         self.setDLParam('pn', 1)
                    807:         # unmark
                    808:         self.setDLParam('mk', None)
                    809:         return self.display()
                    810:     
                    811:     def dl_LastPage(self):
                    812:         """last page action"""
                    813:         self.setDLParam('pn', self.getPT())
                    814:         # unmark
                    815:         self.setDLParam('mk', None)
                    816:         return self.display()
                    817: 
1.32      dwinter   818:     def dl_db(self,db):
                    819:         """set db"""
                    820:         self.setDLParam('db',db)
                    821:         self.display()
1.1       dwinter   822: 
1.18      casties   823:     def changeZogiLibForm(self):
                    824:         """Main configuration"""
                    825:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/changeZogiLibForm.zpt')).__of__(self)
                    826:         return pt()
1.1       dwinter   827:     
1.37      casties   828:     def changeZogiLib(self,title,dlServerURL, version, basePath, dlTarget, dlToolbarBaseURL, RESPONSE=None):
1.18      casties   829:         """change it"""
                    830:         self.title=title
1.35      casties   831:         self.dlServerURL=dlServerURL
1.21      casties   832:         self.basePath = basePath
1.18      casties   833:         self.layout=version
1.44      casties   834:         self.dlTarget = dlTarget
1.3       dwinter   835: 
1.37      casties   836:         if dlToolbarBaseURL:
                    837:             self.dlToolbarBaseURL = dlToolbarBaseURL
                    838:         else:
                    839:             self.dlToolbarBaseURL = dlServerURL + "/digimage.jsp?"
                    840: 
1.18      casties   841:         if RESPONSE is not None:
                    842:             RESPONSE.redirect('manage_main')
1.8       dwinter   843: 
1.35      casties   844: 
                    845: 
                    846:     ##
1.44      casties   847:     ## odds and ends
1.35      casties   848:     ##
                    849: 
                    850:     def repairZogilib(self, obj=None):
                    851:         """change stuff that broke on upgrading"""
                    852: 
                    853:         msg = ""
                    854: 
                    855:         if not obj:
                    856:             obj = self.getPhysicalRoot()
                    857: 
                    858:         print "starting in ", obj
                    859:         
                    860:         entries=obj.ZopeFind(obj,obj_metatypes=['zogiLib'],search_sub=1)
                    861: 
                    862:         for entry in entries:
                    863:             print "  found ", entry
1.37      casties   864:             #
                    865:             # replace digilibBaseUrl by dlServerURL
1.35      casties   866:             if hasattr(entry[1], 'digilibBaseUrl'):
1.37      casties   867:                 msg += "  fixing digilibBaseUrl in "+entry[0]+"\n"
1.36      casties   868:                 entry[1].dlServerURL = re.sub('/servlet/Scaler\?','',entry[1].digilibBaseUrl)
1.35      casties   869:                 del entry[1].digilibBaseUrl
                    870:                 
1.37      casties   871:             #
                    872:             # add dlToolbarBaseURL
                    873:             if not hasattr(entry[1], 'dlToolbarBaseURL'):
                    874:                 msg += "  fixing dlToolbarBaseURL in "+entry[0]+"\n"
                    875:                 entry[1].dlToolbarBaseURL = entry[1].dlServerURL + "/digimage.jsp?"
                    876:                 
1.35      casties   877:         return msg+"\n\nfixed all zogilib instances in: "+obj.title
                    878: 
1.8       dwinter   879:           
1.1       dwinter   880: def manage_addZogiLibForm(self):
                    881:     """interface for adding zogilib"""
1.18      casties   882:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiLibForm')).__of__(self)
1.1       dwinter   883:     return pt()
                    884: 
1.44      casties   885: def manage_addZogiLib(self,id,title,dlServerURL,version="book",basePath="",dlTarget=None,dlToolbarBaseURL=None,RESPONSE=None):
1.1       dwinter   886:     """add dgilib"""
1.43      casties   887:     newObj=zogiLib(id,title,dlServerURL, version, basePath, dlTarget, dlToolbarBaseURL)
1.1       dwinter   888:     self.Destination()._setObject(id,newObj)
                    889:     if RESPONSE is not None:
                    890:         RESPONSE.redirect('manage_main')
1.29      casties   891: 
                    892: 
                    893: class zogiLibPageTemplate(ZopePageTemplate):
                    894:     """pageTemplate Objekt"""
                    895:     meta_type="zogiLib_pageTemplate"
                    896: 
                    897: 
                    898: ## def __init__(self, id, text=None, contentType=None):
                    899: ##         self.id = str(id)
                    900: ##         self.ZBindings_edit(self._default_bindings)
                    901: ##         if text is None:
                    902: ##             text = open(self._default_cont).read()
                    903: ##         self.pt_edit(text, contentType)
                    904: 
                    905: def manage_addZogiLibPageTemplateForm(self):
                    906:     """Form for adding"""
                    907:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiLibPageTemplateForm')).__of__(self)
                    908:     return pt()
                    909: 
1.63      casties   910: def manage_addZogiLibPageTemplate(self, title=None, layout=None, text=None,
1.29      casties   911:                            REQUEST=None, submit=None):
                    912:     "Add a Page Template with optional file content."
                    913: 
1.62      casties   914:     if not layout: layout = "book"
1.63      casties   915:     id = 'main_%s'%layout
1.29      casties   916:     self._setObject(id, zogiLibPageTemplate(id))
                    917:     ob = getattr(self, id)
1.68      casties   918:     ob.pt_edit(open(os.path.join(package_home(globals()),'zpt/main_%s.zpt'%layout)).read(),'text/html')
1.29      casties   919:     if title:
                    920:         ob.pt_setTitle(title)
                    921:     try:
1.63      casties   922:         url = self.DestinationURL()
1.29      casties   923:     except AttributeError:
1.63      casties   924:         url = REQUEST['URL1']
1.29      casties   925:         
1.63      casties   926:     url = "%s/%s" % (url, urllib.quote(id))
                    927:     REQUEST.RESPONSE.redirect(url+'/manage_main')
1.29      casties   928:     return ''
1.35      casties   929: 

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