Annotation of zogiLib/zogiLib.py, revision 1.54

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

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