Annotation of zogiLib/zogiLib.py, revision 1.57

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

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