Annotation of zogiLib/zogiLib.py, revision 1.53

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

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