Annotation of zogiLib/zogiLib.py, revision 1.56

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

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