Annotation of zogiLib/zogiLib.py, revision 1.22

1.13      casties     1: 
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
                      6: from webdav.common import rfc1123_date
1.1       dwinter     7: 
                      8: import xml.dom.minidom
                      9: from OFS.Folder import Folder
                     10: from xml_helpers import getText
                     11: import os
                     12: import re
                     13: import string
                     14: import urllib
                     15: from Globals import package_home
                     16: 
                     17: def getString(self,key,default=''):
                     18:     try:
                     19:         return self.REQUEST[key]
                     20:     except:
                     21:         return default
                     22: 
1.15      casties    23: def sendFile(self, filename, type):
1.17      casties    24:     """sends an object or a local file (in the product) as response"""
                     25:     paths = filename.split('/')
                     26:     object = self
                     27:     # look for an object called filename
                     28:     for path in paths:
                     29:         if hasattr(object, path):
                     30:        object = getattr(object, path)
                     31:    else:
                     32:        object = None
                     33:        break
                     34:     if object:
1.18      casties    35:    # if the object exists then send it
                     36:    return object.index_html(self.REQUEST.REQUEST, self.REQUEST.RESPONSE)
1.17      casties    37:     else:
                     38:    # send a local file with the given content-type
                     39:    fn = os.path.join(package_home(globals()), filename)
                     40:    self.REQUEST.RESPONSE.setHeader("Content-Type", type)
                     41:    self.REQUEST.RESPONSE.write(file(fn).read())
1.15      casties    42:     return
                     43: 
1.18      casties    44: class BrowserCheck:
                     45:     """check the browsers request to find out the browser type"""
1.5       dwinter    46:     
1.18      casties    47:     def __init__(self, zope):
                     48:    self.ua = zope.REQUEST.get_header("HTTP_USER_AGENT")
                     49:    self.isIE = string.find(self.ua, 'MSIE') > -1
                     50:    self.isN4 = (string.find(self.ua, 'Mozilla/4.') > -1) and not self.isIE
                     51:    self.nav = self.ua[string.find(self.ua, '('):]
                     52:    ie = string.split(self.nav, "; ")[1]
                     53:    if string.find(ie, "MSIE") > -1:
                     54:        self.versIE = string.split(ie, " ")[1]
                     55:    self.isMac = string.find(self.ua, 'Macintosh') > -1
                     56:    self.isWin = string.find(self.ua, 'Windows') > -1
                     57:    self.isIEWin = self.isIE and self.isWin
                     58:    self.isIEMac = self.isIE and self.isMac
1.5       dwinter    59: 
                     60: 
                     61: def manage_addZogiLibMainTemplateForm(self):
                     62:     """Form for adding"""
1.18      casties    63:     #FIXME:???
                     64:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/AddOSAS_thumbTemplate.zpt')).__of__(self)
1.5       dwinter    65:     return pt()
                     66: 
                     67: 
                     68: 
                     69: 
                     70: def manage_addZogiLibMainTemplate(self, id,title=None, text=None,
                     71:                            REQUEST=None, submit=None):
                     72:     "Add a Page Template with optional file content."
1.18      casties    73:     #FIXME:???
1.5       dwinter    74:     id = str(id)
                     75:     if REQUEST is None:
                     76:         self._setObject(id, zogiLib_mainTemplate(id, text))
                     77:         ob = getattr(self, id)
                     78:        
                     79:         if title:
                     80:             ob.pt_setTitle(title)
                     81:         return ob
                     82:     else:
                     83:         file = REQUEST.form.get('file')
                     84:         headers = getattr(file, 'headers', None)
                     85:         if headers is None or not file.filename:
                     86:             zpt = zogiLib_mainTemplate(id)
                     87:         else:
                     88:             zpt = zogiLib_mainTemplate(id, file, headers.get('content_type'))
                     89: 
                     90:         self._setObject(id, zpt)
                     91:         ob = getattr(self, id)
                     92: 
                     93: 
                     94:         try:
                     95:             u = self.DestinationURL()
                     96:         except AttributeError:
                     97:             u = REQUEST['URL1']
                     98: 
                     99:         if submit == " Add and Edit ":
                    100:             u = "%s/%s" % (u, quote(id))
                    101:         REQUEST.RESPONSE.redirect(u+'/manage_main')
                    102:     return ''
                    103: 
1.1       dwinter   104:     
1.4       dwinter   105: class zogiImage(Image):
                    106:     """einzelnes Image"""
                    107:     meta_type="zogiImage"
                    108: 
1.18      casties   109:     manage_options=ZopePageTemplate.manage_options+(
                    110:         {'label':'Main config','action':'changeZogiImageForm'},
                    111:        )
1.4       dwinter   112:     
                    113:     
                    114:     def __init__(self,id,title,baseUrl,queryString,content_type='',precondition=''):
                    115:         """init"""
                    116:         self.id=id
                    117:         self.title=title
                    118:         self.baseUrl=baseUrl
                    119:         self.queryString=queryString
                    120:         self.content_type=content_type
                    121:         self.precondition=precondition
                    122: 
                    123:     def getData(self):
                    124:         """getUrlData"""
                    125:         return urllib.urlopen(self.baseUrl+self.queryString)
                    126: 
                    127:     def changeZogiImageForm(self):
                    128:         """Main configuration"""
1.18      casties   129:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/changeZogiImageForm.zpt')).__of__(self)
1.4       dwinter   130:         return pt()
                    131:     
                    132:     def changeZogiImage(self,title,baseUrl, queryString,RESPONSE=None):
                    133:         """change it"""
                    134:         self.title=title
                    135:         self.baseUrl=baseUrl
                    136:         self.queryString=queryString
                    137: 
                    138:         if RESPONSE is not None:
                    139:             RESPONSE.redirect('manage_main')
                    140: 
                    141:         
                    142:     def index_html(self, REQUEST, RESPONSE):
                    143:         """
                    144:         Modified version of OFS/Image.py
1.1       dwinter   145:         
1.4       dwinter   146:         The default view of the contents of a File or Image.
                    147: 
                    148:         Returns the contents of the file or image.  Also, sets the
                    149:         Content-Type HTTP header to the objects content type.
                    150:         """
                    151: 
                    152:         # HTTP If-Modified-Since header handling.
                    153:         header=REQUEST.get_header('If-Modified-Since', None)
                    154:         if header is not None:
                    155:             header=header.split( ';')[0]
                    156:             # Some proxies seem to send invalid date strings for this
                    157:             # header. If the date string is not valid, we ignore it
                    158:             # rather than raise an error to be generally consistent
                    159:             # with common servers such as Apache (which can usually
                    160:             # understand the screwy date string as a lucky side effect
                    161:             # of the way they parse it).
                    162:             # This happens to be what RFC2616 tells us to do in the face of an
                    163:             # invalid date.
                    164:             try:    mod_since=long(DateTime(header).timeTime())
                    165:             except: mod_since=None
                    166:             if mod_since is not None:
                    167:                 if self._p_mtime:
                    168:                     last_mod = long(self._p_mtime)
                    169:                 else:
                    170:                     last_mod = long(0)
                    171:                 if last_mod > 0 and last_mod <= mod_since:
                    172:                     # Set header values since apache caching will return Content-Length
                    173:                     # of 0 in response if size is not set here
                    174:                     RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
                    175:                     RESPONSE.setHeader('Content-Type', self.content_type)
                    176:                     RESPONSE.setHeader('Content-Length', self.size)
                    177:                     RESPONSE.setHeader('Accept-Ranges', 'bytes')
                    178:                     self.ZCacheable_set(None)
                    179:                     RESPONSE.setStatus(304)
                    180:                     return ''
                    181: 
                    182:         if self.precondition and hasattr(self,self.precondition):
                    183:             # Grab whatever precondition was defined and then
                    184:             # execute it.  The precondition will raise an exception
                    185:             # if something violates its terms.
                    186:             c=getattr(self,self.precondition)
                    187:             if hasattr(c,'isDocTemp') and c.isDocTemp:
                    188:                 c(REQUEST['PARENTS'][1],REQUEST)
                    189:             else:
                    190:                 c()
                    191: 
                    192:         # HTTP Range header handling
                    193:         range = REQUEST.get_header('Range', None)
                    194:         request_range = REQUEST.get_header('Request-Range', None)
                    195:         if request_range is not None:
                    196:             # Netscape 2 through 4 and MSIE 3 implement a draft version
                    197:             # Later on, we need to serve a different mime-type as well.
                    198:             range = request_range
                    199:         if_range = REQUEST.get_header('If-Range', None)
                    200:         if range is not None:
                    201:             ranges = HTTPRangeSupport.parseRange(range)
                    202: 
                    203:             if if_range is not None:
                    204:                 # Only send ranges if the data isn't modified, otherwise send
                    205:                 # the whole object. Support both ETags and Last-Modified dates!
                    206:                 if len(if_range) > 1 and if_range[:2] == 'ts':
                    207:                     # ETag:
                    208:                     if if_range != self.http__etag():
                    209:                         # Modified, so send a normal response. We delete
                    210:                         # the ranges, which causes us to skip to the 200
                    211:                         # response.
                    212:                         ranges = None
                    213:                 else:
                    214:                     # Date
                    215:                     date = if_range.split( ';')[0]
                    216:                     try: mod_since=long(DateTime(date).timeTime())
                    217:                     except: mod_since=None
                    218:                     if mod_since is not None:
                    219:                         if self._p_mtime:
                    220:                             last_mod = long(self._p_mtime)
                    221:                         else:
                    222:                             last_mod = long(0)
                    223:                         if last_mod > mod_since:
                    224:                             # Modified, so send a normal response. We delete
                    225:                             # the ranges, which causes us to skip to the 200
                    226:                             # response.
                    227:                             ranges = None
                    228: 
                    229:             if ranges:
                    230:                 # Search for satisfiable ranges.
                    231:                 satisfiable = 0
                    232:                 for start, end in ranges:
                    233:                     if start < self.size:
                    234:                         satisfiable = 1
                    235:                         break
                    236: 
                    237:                 if not satisfiable:
                    238:                     RESPONSE.setHeader('Content-Range',
                    239:                         'bytes */%d' % self.size)
                    240:                     RESPONSE.setHeader('Accept-Ranges', 'bytes')
                    241:                     RESPONSE.setHeader('Last-Modified',
                    242:                         rfc1123_date(self._p_mtime))
                    243:                     RESPONSE.setHeader('Content-Type', self.content_type)
                    244:                     RESPONSE.setHeader('Content-Length', self.size)
                    245:                     RESPONSE.setStatus(416)
                    246:                     return ''
                    247: 
                    248:                 ranges = HTTPRangeSupport.expandRanges(ranges, self.size)
                    249:                                 
                    250:                 if len(ranges) == 1:
                    251:                     # Easy case, set extra header and return partial set.
                    252:                     start, end = ranges[0]
                    253:                     size = end - start
                    254: 
                    255:                     RESPONSE.setHeader('Last-Modified',
                    256:                         rfc1123_date(self._p_mtime))
                    257:                     RESPONSE.setHeader('Content-Type', self.content_type)
                    258:                     RESPONSE.setHeader('Content-Length', size)
                    259:                     RESPONSE.setHeader('Accept-Ranges', 'bytes')
                    260:                     RESPONSE.setHeader('Content-Range',
                    261:                         'bytes %d-%d/%d' % (start, end - 1, self.size))
                    262:                     RESPONSE.setStatus(206) # Partial content
                    263: 
                    264:                     data = urllib.urlopen(self.baseUrl+self.queryString).read()
                    265:                     if type(data) is StringType:
                    266:                         return data[start:end]
                    267: 
                    268:                     # Linked Pdata objects. Urgh.
                    269:                     pos = 0
                    270:                     while data is not None:
                    271:                         l = len(data.data)
                    272:                         pos = pos + l
                    273:                         if pos > start:
                    274:                             # We are within the range
                    275:                             lstart = l - (pos - start)
                    276: 
                    277:                             if lstart < 0: lstart = 0
                    278: 
                    279:                             # find the endpoint
                    280:                             if end <= pos:
                    281:                                 lend = l - (pos - end)
                    282: 
                    283:                                 # Send and end transmission
                    284:                                 RESPONSE.write(data[lstart:lend])
                    285:                                 break
                    286: 
                    287:                             # Not yet at the end, transmit what we have.
                    288:                             RESPONSE.write(data[lstart:])
                    289: 
                    290:                         data = data.next
                    291: 
                    292:                     return ''
                    293: 
                    294:                 else:
                    295:                     boundary = choose_boundary()
                    296: 
                    297:                     # Calculate the content length
                    298:                     size = (8 + len(boundary) + # End marker length
                    299:                         len(ranges) * (         # Constant lenght per set
                    300:                             49 + len(boundary) + len(self.content_type) +
                    301:                             len('%d' % self.size)))
                    302:                     for start, end in ranges:
                    303:                         # Variable length per set
                    304:                         size = (size + len('%d%d' % (start, end - 1)) +
                    305:                             end - start)
                    306: 
                    307: 
                    308:                     # Some clients implement an earlier draft of the spec, they
                    309:                     # will only accept x-byteranges.
                    310:                     draftprefix = (request_range is not None) and 'x-' or ''
                    311: 
                    312:                     RESPONSE.setHeader('Content-Length', size)
                    313:                     RESPONSE.setHeader('Accept-Ranges', 'bytes')
                    314:                     RESPONSE.setHeader('Last-Modified',
                    315:                         rfc1123_date(self._p_mtime))
                    316:                     RESPONSE.setHeader('Content-Type',
                    317:                         'multipart/%sbyteranges; boundary=%s' % (
                    318:                             draftprefix, boundary))
                    319:                     RESPONSE.setStatus(206) # Partial content
                    320: 
                    321:                     data = urllib.urlopen(self.baseUrl+self.queryString).read()
                    322:                     # The Pdata map allows us to jump into the Pdata chain
                    323:                     # arbitrarily during out-of-order range searching.
                    324:                     pdata_map = {}
                    325:                     pdata_map[0] = data
                    326: 
                    327:                     for start, end in ranges:
                    328:                         RESPONSE.write('\r\n--%s\r\n' % boundary)
                    329:                         RESPONSE.write('Content-Type: %s\r\n' %
                    330:                             self.content_type)
                    331:                         RESPONSE.write(
                    332:                             'Content-Range: bytes %d-%d/%d\r\n\r\n' % (
                    333:                                 start, end - 1, self.size))
                    334: 
                    335:                         if type(data) is StringType:
                    336:                             RESPONSE.write(data[start:end])
                    337: 
                    338:                         else:
                    339:                             # Yippee. Linked Pdata objects. The following
                    340:                             # calculations allow us to fast-forward through the
                    341:                             # Pdata chain without a lot of dereferencing if we
                    342:                             # did the work already.
                    343:                             first_size = len(pdata_map[0].data)
                    344:                             if start < first_size:
                    345:                                 closest_pos = 0
                    346:                             else:
                    347:                                 closest_pos = (
                    348:                                     ((start - first_size) >> 16 << 16) +
                    349:                                     first_size)
                    350:                             pos = min(closest_pos, max(pdata_map.keys()))
                    351:                             data = pdata_map[pos]
                    352: 
                    353:                             while data is not None:
                    354:                                 l = len(data.data)
                    355:                                 pos = pos + l
                    356:                                 if pos > start:
                    357:                                     # We are within the range
                    358:                                     lstart = l - (pos - start)
                    359: 
                    360:                                     if lstart < 0: lstart = 0
                    361: 
                    362:                                     # find the endpoint
                    363:                                     if end <= pos:
                    364:                                         lend = l - (pos - end)
                    365: 
                    366:                                         # Send and loop to next range
                    367:                                         RESPONSE.write(data[lstart:lend])
                    368:                                         break
                    369: 
                    370:                                     # Not yet at the end, transmit what we have.
                    371:                                     RESPONSE.write(data[lstart:])
                    372: 
                    373:                                 data = data.next
                    374:                                 # Store a reference to a Pdata chain link so we
                    375:                                 # don't have to deref during this request again.
                    376:                                 pdata_map[pos] = data
                    377: 
                    378:                     # Do not keep the link references around.
                    379:                     del pdata_map
                    380: 
                    381:                     RESPONSE.write('\r\n--%s--\r\n' % boundary)
                    382:                     return ''
                    383: 
                    384:         RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
                    385:         RESPONSE.setHeader('Content-Type', self.content_type)
                    386:         RESPONSE.setHeader('Content-Length', self.size)
                    387:         RESPONSE.setHeader('Accept-Ranges', 'bytes')
                    388: 
                    389:         # Don't cache the data itself, but provide an opportunity
                    390:         # for a cache manager to set response headers.
                    391:         self.ZCacheable_set(None)
                    392: 
                    393:         data=urllib.urlopen(self.baseUrl+self.queryString).read()
                    394:         
                    395:         if type(data) is type(''): 
                    396:             RESPONSE.setBase(None)
                    397:             return data
                    398: 
                    399:         while data is not None:
                    400:             RESPONSE.write(data.data)
                    401:             data=data.next
                    402: 
                    403:         return ''
                    404: 
                    405: 
                    406: def manage_addZogiImageForm(self):
                    407:     """Form for adding"""
1.18      casties   408:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiImage.zpt')).__of__(self)
1.4       dwinter   409:     return pt()
                    410: 
                    411: 
                    412: def manage_addZogiImage(self,id,title,baseUrl, queryString,RESPONSE=None):
                    413:     """add dgilib"""
                    414:     newObj=zogiImage(id,title,baseUrl, queryString)
                    415:     self.Destination()._setObject(id,newObj)
                    416:     if RESPONSE is not None:
                    417:         RESPONSE.redirect('manage_main')
                    418: 
                    419: 
                    420: 
1.1       dwinter   421: class zogiLib(Folder):
                    422:     """StandardElement"""
                    423: 
                    424:     meta_type="zogiLib"
                    425: 
1.18      casties   426:     manage_options = Folder.manage_options+(
                    427:             {'label':'Main Config','action':'changeZogiLibForm'},
                    428:             )
1.1       dwinter   429: 
1.21      casties   430:     def __init__(self, id, title, digilibBaseUrl, localFileBase, version="book", basePath=""):
1.1       dwinter   431:         """init"""
                    432: 
                    433:         self.id=id
                    434:         self.title=title
1.2       dwinter   435:         self.digilibBaseUrl=digilibBaseUrl
1.1       dwinter   436:         self.localFileBase=localFileBase
1.21      casties   437:         self.basePath=basePath
1.18      casties   438:         self.layout=version
1.1       dwinter   439: 
                    440: 
1.18      casties   441:     def getDLInfo(self):
                    442:         """get DLInfo from digilib server"""
                    443:         paramH={}
                    444:         baseUrl=re.sub("servlet/Scaler","dlInfo-xml.jsp",self.digilibBaseUrl)
                    445:         try:
                    446:             url=urllib.urlopen(baseUrl+self.REQUEST['QUERY_STRING'])
                    447:             dom=xml.dom.minidom.parse(url)
                    448:             params=dom.getElementsByTagName('parameter')
                    449:             for param in params:
                    450:                 paramH[param.getAttribute('name')]=param.getAttribute('value')
                    451:             return paramH
                    452:         except:
                    453:             return null
                    454: 
1.1       dwinter   455: 
1.18      casties   456:     def createHeadJS(self):
1.19      casties   457:         """generate all javascript tags for head"""
1.18      casties   458:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/zogilib_head_js')).__of__(self)
                    459:         return pt()
1.19      casties   460: 
                    461:     def createParamJS(self):
                    462:         """generate javascript for parameters only"""
                    463:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/zogilib_param_js')).__of__(self)
                    464:         return pt()
                    465:         
1.3       dwinter   466:                         
1.21      casties   467:     def createScalerImg(self, requestString = None, bottom = 0, side = 0):
1.18      casties   468:         """generate Scaler IMG Tag"""
1.20      casties   469:    self.checkQuery()
                    470:    bt = self.REQUEST.SESSION['browserType']
1.18      casties   471:         if not requestString:
1.21      casties   472:             requestString = self.getAllDLParams()
1.18      casties   473:         url = self.digilibBaseUrl+requestString
                    474:         tag = ""
                    475:         if bt.isN4:
                    476:             tag += '<ilayer id="scaler">'
                    477:         else:
                    478:             tag += '<div id="scaler">'
                    479:         tag += '<script type="text/javascript">'
                    480:         tag += "var ps = bestPicSize(getElement('scaler'));"
1.21      casties   481:         b_par = ""
                    482:         s_par = ""
                    483:         if (bottom != 0) or (side != 0):
                    484:             b_par = "-" + str(int(bottom))
                    485:             s_par = "-" + str(int(side))
                    486:         tag += 'document.write(\'<img id="pic" src="%s&dw=\'+(ps.width%s)+\'&dh=\'+(ps.height%s)+\'" />\')'%(url, s_par, b_par)
1.18      casties   487:         tag += '</script>'
                    488:         if bt.isN4:
                    489:             tag += '</ilayer>'
                    490:         else:
                    491:             tag += '</div>'
                    492:         return tag
1.1       dwinter   493: 
1.18      casties   494:     def createAuxDiv(self):
                    495:         """generate other divs"""
1.20      casties   496:    self.checkQuery()
                    497:    bt = self.REQUEST.SESSION['browserType']
1.18      casties   498:         if bt.isN4:
                    499:             f = 'zpt/zogilib_divsN4.zpt'
                    500:         else:
                    501:             f = 'zpt/zogilib_divs.zpt'
                    502:         pt=PageTemplateFile(os.path.join(package_home(globals()),f)).__of__(self)
                    503:         return pt()
1.3       dwinter   504: 
1.9       dwinter   505: 
1.18      casties   506:     def option_js(self):
                    507:         """option_js"""
                    508:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/option_js')).__of__(self)
                    509:         return pt()
1.1       dwinter   510: 
1.18      casties   511:     def dl_lib_js(self):
                    512:         """javascript"""
                    513:         return sendFile(self, 'js/dl_lib.js', 'text/plain')
                    514: 
                    515:     def js_lib_js(self):
                    516:         """javascript"""
                    517:         return sendFile(self, 'js/js_lib.js', 'text/plain')
1.1       dwinter   518: 
1.18      casties   519:     def optionwindow(self):
                    520:         """showoptions"""
                    521:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/optionwindow.zpt')).__of__(self)
                    522:         return pt()
1.13      casties   523: 
                    524:     def mark1(self):
                    525:         """mark image"""
1.18      casties   526:         return sendFile(self, 'images/mark1.gif', 'image/gif')
1.13      casties   527: 
                    528:     def mark2(self):
                    529:         """mark image"""
1.18      casties   530:         return sendFile(self, 'images/mark2.gif', 'image/gif')
1.13      casties   531: 
                    532:     def mark3(self):
                    533:         """mark image"""
1.18      casties   534:         return sendFile(self, 'images/mark3.gif', 'image/gif')
1.13      casties   535: 
                    536:     def mark4(self):
                    537:         """mark image"""
1.18      casties   538:         return sendFile(self, 'images/mark4.gif', 'image/gif')
1.13      casties   539: 
                    540:     def mark5(self):
                    541:         """mark image"""
1.18      casties   542:         return sendFile(self, 'images/mark5.gif', 'image/gif')
1.13      casties   543: 
                    544:     def mark6(self):
                    545:         """mark image"""
1.18      casties   546:         return sendFile(self, 'images/mark6.gif', 'image/gif')
1.13      casties   547: 
                    548:     def mark7(self):
                    549:         """mark image"""
1.18      casties   550:         return sendFile(self, 'images/mark7.gif', 'image/gif')
1.13      casties   551: 
                    552:     def mark8(self):
                    553:         """mark image"""
1.18      casties   554:         return sendFile(self, 'images/mark8.gif', 'image/gif')
1.13      casties   555: 
                    556:     def corner1(self):
                    557:         """mark image"""
1.18      casties   558:         return sendFile(self, 'images/olinks.gif', 'image/gif')
1.13      casties   559: 
                    560:     def corner2(self):
                    561:         """mark image"""
1.18      casties   562:         return sendFile(self, 'images/orechts.gif', 'image/gif')
1.13      casties   563: 
                    564:     def corner3(self):
                    565:         """mark image"""
1.18      casties   566:         return sendFile(self, 'images/ulinks.gif', 'image/gif')
1.13      casties   567: 
                    568:     def corner4(self):
                    569:         """mark image"""
1.18      casties   570:         return sendFile(self, 'images/urechts.gif', 'image/gif')
1.13      casties   571: 
1.22    ! casties   572:     def up_img(self):
        !           573:         """mark image"""
        !           574:         return sendFile(self, 'images/up.gif', 'image/gif')
        !           575: 
        !           576:     def down_img(self):
        !           577:         """mark image"""
        !           578:         return sendFile(self, 'images/down.gif', 'image/gif')
        !           579: 
        !           580:     def left_img(self):
        !           581:         """mark image"""
        !           582:         return sendFile(self, 'images/left.gif', 'image/gif')
        !           583: 
        !           584:     def right_img(self):
        !           585:         """mark image"""
        !           586:         return sendFile(self, 'images/right.gif', 'image/gif')
1.13      casties   587: 
                    588: 
1.1       dwinter   589:             
                    590:     def index_html(self):
                    591:         """main action"""
1.18      casties   592:         tp = "zogiLibMainTemplate"
                    593:         if hasattr(self, tp):
                    594:        pt = getattr(self, tp)
                    595:         else:
                    596:             pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/zogiLibMain_%s'%self.layout)).__of__(self)
                    597:         return pt()
                    598: 
1.1       dwinter   599: 
                    600: 
1.21      casties   601:     def storeQuery(self, more = None):
1.1       dwinter   602:         """storeQuery in session"""
1.18      casties   603:         dlParams = {}
1.1       dwinter   604:         for fm in self.REQUEST.form.keys():
1.18      casties   605:             dlParams[fm] = self.REQUEST.form[fm]
1.21      casties   606:         # look for more
                    607:         if more:
                    608:             for fm in more.split('&'):
                    609:                 try:
                    610:                     pv = fm.split('=')
                    611:                     dlParams[pv[0]] = pv[1]
                    612:                 except:
                    613:                     print "ouch!"
                    614:         # parse digilib mode parameter
1.18      casties   615:         if 'mo' in dlParams:
                    616:             if len(dlParams['mo']) > 0:
                    617:                 modes=dlParams['mo'].split(',')
                    618:         else:
                    619:             modes=[]
                    620: 
                    621:         self.REQUEST.SESSION['query'] = dlParams
                    622:         self.REQUEST.SESSION['dlModes'] = modes
                    623:         self.REQUEST.SESSION['dlInfo'] = self.getDLInfo()
                    624:         self.REQUEST.SESSION['browserType'] = BrowserCheck(self)
1.1       dwinter   625: 
1.20      casties   626:     def checkQuery(self):
                    627:    """check if the query has been stored"""
1.21      casties   628:    if not (self.REQUEST.SESSION):
1.20      casties   629:        print "ZOGILIB: have to store query!!"
                    630:        storeQuery(self)
1.3       dwinter   631:         
1.22    ! casties   632:     def getDLParam(self, param):
1.18      casties   633:         """returns parameter"""
1.3       dwinter   634:         try:
                    635:             return self.REQUEST.SESSION['query'][param]
                    636:         except:
                    637:             return None
                    638: 
1.18      casties   639:     def setDLParam(self, param, value):
                    640:         """sets parameter"""
                    641:         self.REQUEST.SESSION['query'][param] = value
                    642:         return
                    643: 
                    644:     def getAllDLParams(self):
                    645:         """parameter string for digilib"""
                    646:         dlParams = self.REQUEST.SESSION['query']
                    647:         # save modes
                    648:         modes = self.REQUEST.SESSION['dlModes']
                    649:         dlParams['mo'] = string.join(modes, ',')
                    650:         # assemble query string
                    651:         ret = ""
                    652:         for param in dlParams.keys():
                    653:             val = str(dlParams[param])
                    654:             if val != "":
                    655:                 ret += param + "=" + val + "&"
                    656:         # omit trailing "&"
                    657:         return ret.rstrip('&')
                    658: 
                    659:         
                    660:     def setDLParams(self,pn=None,ws=None,rot=None,brgt=None,cont=None):
                    661:         """setze Parameter"""
                    662:         ret=""
                    663: 
                    664:         if brgt:
                    665:             self.setDLParam('brgt', brgt)
                    666: 
                    667:         if cont:
                    668:             self.setDLParam('cont', cont)
                    669: 
                    670:         if pn:
1.21      casties   671:             # unmark
                    672:             self.setDLParam('mk', None)
1.18      casties   673:             self.setDLParam('pn', pn)
                    674: 
                    675:         if ws:
                    676:             self.setDLParam('ws', ws)
                    677: 
                    678:         if rot:
                    679:             self.setDLParam('rot', rot)
                    680:             
                    681:         return self.display()
                    682: 
                    683: 
                    684:     def display(self):
                    685:         """(re)display page"""
                    686:         params = self.getAllDLParams()
1.21      casties   687:         if self.basePath:
                    688:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL2']+'?'+params)
                    689:         else:
                    690:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'?'+params)
1.18      casties   691: 
                    692:     def getPT(self):
                    693:         """pagenums"""
                    694:         di = self.REQUEST.SESSION['dlInfo']
                    695:         if di:
                    696:             return int(di['pt'])
                    697:         else:
                    698:             return 1
                    699:     
                    700:     def getPN(self):
                    701:         """Pagenum"""
                    702:         pn = self.getDLParam('pn')
1.3       dwinter   703:         if pn:
1.18      casties   704:             return int(pn)
1.3       dwinter   705:         else:
                    706:             return 1
                    707: 
1.18      casties   708:     def getBiggerWS(self):
1.3       dwinter   709:         """ws+1"""
1.18      casties   710:         ws=self.getDLParam('ws')
1.3       dwinter   711:         if ws:
                    712:             return int(ws)+1
                    713:         else:
                    714:             return 2
                    715:         
1.18      casties   716:     def getSmallerWS(self):
                    717:         """ws-1"""
                    718:         ws=self.getDLParam('ws')
1.3       dwinter   719:         if ws:
                    720:             if int(ws)==1:
1.12      casties   721:                 return 1
1.3       dwinter   722:             else:
                    723:                 return int(ws)-1
                    724:         else:
                    725:             return 1
1.1       dwinter   726: 
1.18      casties   727:     def hasMode(self, mode):
                    728:         """returns if mode is in the diglib mo parameter"""
                    729:         return (mode in self.REQUEST.SESSION['dlModes'])
                    730: 
                    731:     def hasNextPage(self):
                    732:         """returns if there is a next page"""
                    733:         pn = self.getPN()
                    734:         pt = self.getPT()
                    735:         return (pn < pt)
                    736:    
                    737:     def hasPrevPage(self):
                    738:         """returns if there is a previous page"""
                    739:         pn = self.getPN()
                    740:         return (pn > 1)
1.1       dwinter   741: 
1.22    ! casties   742:     def canMoveLeft(self):
        !           743:         """returns if its possible to move left"""
        !           744:         wx = float(self.getDLParam('wx') or 0)
        !           745:         return (wx > 0)
        !           746: 
        !           747:     def canMoveRight(self):
        !           748:         """returns if its possible to move right"""
        !           749:         wx = float(self.getDLParam('wx') or 0)
        !           750:         ww = float(self.getDLParam('ww') or 1)
        !           751:         return (wx + ww < 1)
        !           752: 
        !           753:     def canMoveUp(self):
        !           754:         """returns if its possible to move up"""
        !           755:         wy = float(self.getDLParam('wy') or 0)
        !           756:         return (wy > 0)
        !           757: 
        !           758:     def canMoveDown(self):
        !           759:         """returns if its possible to move down"""
        !           760:         wy = float(self.getDLParam('wy') or 0)
        !           761:         wh = float(self.getDLParam('wh') or 1)
        !           762:         return (wy + wh < 1)
        !           763: 
1.1       dwinter   764:         
1.18      casties   765:     def dl_HMirror(self):
                    766:         """mirror action"""
                    767:         modes = self.REQUEST.SESSION['dlModes']
                    768:         if 'hmir' in modes:
                    769:             modes.remove('hmir')
                    770:         else:
                    771:             modes.append('hmir')
1.1       dwinter   772: 
1.18      casties   773:         return self.display()
                    774:        
                    775:     def dl_VMirror(self):
                    776:         """mirror action"""
                    777:         modes = self.REQUEST.SESSION['dlModes']
                    778:         if 'vmir' in modes:
                    779:             modes.remove('vmir')
                    780:         else:
                    781:             modes.append('vmir')
1.1       dwinter   782: 
1.18      casties   783:         return self.display()
1.1       dwinter   784: 
1.22    ! casties   785:     def dl_Zoom(self, z):
        !           786:         """general zoom action"""
        !           787:         ww1 = float(self.getDLParam('ww') or 1)
        !           788:         wh1 = float(self.getDLParam('wh') or 1)
        !           789:         wx = float(self.getDLParam('wx') or 0)
        !           790:         wy = float(self.getDLParam('wy') or 0)
        !           791:         ww2 = ww1 * z
        !           792:         wh2 = wh1 * z
        !           793:         wx += (ww1 - ww2) / 2
        !           794:         wy += (wh1 - wh2) / 2
        !           795:         ww2 = max(min(ww2, 1), 0)
        !           796:         wh2 = max(min(wh2, 1), 0)
        !           797:         wx = max(min(wx, 1), 0)
        !           798:         wy = max(min(wy, 1), 0)
        !           799:         self.setDLParam('ww', ww2)
        !           800:         self.setDLParam('wh', wh2)
        !           801:         self.setDLParam('wx', wx)
        !           802:         self.setDLParam('wy', wy)
        !           803:         return self.display()
        !           804:         
        !           805:     def dl_ZoomIn(self):
        !           806:         """zoom in action"""
        !           807:         z = 0.7071
        !           808:         return self.dl_Zoom(z)
        !           809: 
        !           810:     def dl_ZoomOut(self):
        !           811:         """zoom out action"""
        !           812:         z = 1.4142
        !           813:         return self.dl_Zoom(z)
        !           814: 
        !           815:     def dl_Move(self, dx, dy):
        !           816:         """general move action"""
        !           817:         ww = float(self.getDLParam('ww') or 1)
        !           818:         wh = float(self.getDLParam('wh') or 1)
        !           819:         wx = float(self.getDLParam('wx') or 0)
        !           820:         wy = float(self.getDLParam('wy') or 0)
        !           821:         wx += dx * 0.5 * ww
        !           822:         wy += dy * 0.5 * wh
        !           823:         wx = max(min(wx, 1), 0)
        !           824:         wy = max(min(wy, 1), 0)
        !           825:         self.setDLParam('wx', wx)
        !           826:         self.setDLParam('wy', wy)
        !           827:         return self.display()
        !           828:         
        !           829:     def dl_MoveLeft(self):
        !           830:         """move left action"""
        !           831:         return self.dl_Move(-1, 0)
        !           832:     
        !           833:     def dl_MoveRight(self):
        !           834:         """move left action"""
        !           835:         return self.dl_Move(1, 0)
        !           836:     
        !           837:     def dl_MoveUp(self):
        !           838:         """move left action"""
        !           839:         return self.dl_Move(0, -1)
        !           840:     
        !           841:     def dl_MoveDown(self):
        !           842:         """move left action"""
        !           843:         return self.dl_Move(0, 1)
        !           844:     
1.18      casties   845:     def dl_WholePage(self):
                    846:         """zoom out action"""
                    847:         self.setDLParam('ww', 1)
                    848:         self.setDLParam('wh', 1)
                    849:         self.setDLParam('wx', 0)
                    850:         self.setDLParam('wy', 0)
                    851:         return self.display()
                    852:         
                    853:     def dl_PrevPage(self):
                    854:         """next page action"""
                    855:         pn = self.getPN() - 1
                    856:         if pn < 1:
                    857:             pn = 1
                    858:         self.setDLParam('pn', pn)
                    859:         # unmark
                    860:         self.setDLParam('mk', None)
                    861:         return self.display()
                    862:         
                    863:     def dl_NextPage(self):
                    864:         """next page action"""
                    865:         pn = self.getPN() + 1
                    866:         pt = self.getPT()
                    867:         if pn > pt:
                    868:             pn = pt
                    869:         self.setDLParam('pn', pn)
                    870:         # unmark
                    871:         self.setDLParam('mk', None)
                    872:         return self.display()
                    873: 
                    874:     def dl_FirstPage(self):
                    875:         """first page action"""
                    876:         self.setDLParam('pn', 1)
                    877:         # unmark
                    878:         self.setDLParam('mk', None)
                    879:         return self.display()
                    880:     
                    881:     def dl_LastPage(self):
                    882:         """last page action"""
                    883:         self.setDLParam('pn', self.getPT())
                    884:         # unmark
                    885:         self.setDLParam('mk', None)
                    886:         return self.display()
                    887: 
                    888:     def dl_Unmark(self):
                    889:         """action to remove last mark"""
                    890:         mk = self.getDLParam('mk')
                    891:         if mk:
                    892:             marks = mk.split(',')
                    893:             marks.pop()
                    894:             mk = string.join(marks, ',')
                    895:             self.setDLParam('mk', mk)
                    896:         return self.display()
1.1       dwinter   897: 
                    898: 
                    899: 
1.18      casties   900:     def changeZogiLibForm(self):
                    901:         """Main configuration"""
                    902:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/changeZogiLibForm.zpt')).__of__(self)
                    903:         return pt()
1.1       dwinter   904:     
1.21      casties   905:     def changeZogiLib(self,title,digilibBaseUrl, localFileBase, version, basePath, RESPONSE=None):
1.18      casties   906:         """change it"""
                    907:         self.title=title
                    908:         self.digilibBaseUrl=digilibBaseUrl
                    909:         self.localFileBase=localFileBase
1.21      casties   910:         self.basePath = basePath
1.18      casties   911:         self.layout=version
1.3       dwinter   912: 
1.18      casties   913:         if RESPONSE is not None:
                    914:             RESPONSE.redirect('manage_main')
1.8       dwinter   915: 
                    916:           
1.1       dwinter   917: def manage_addZogiLibForm(self):
                    918:     """interface for adding zogilib"""
1.18      casties   919:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiLibForm')).__of__(self)
1.1       dwinter   920:     return pt()
                    921: 
1.21      casties   922: def manage_addZogiLib(self,id,title,digilibBaseUrl, localFileBase,version="book",basePath="",RESPONSE=None):
1.1       dwinter   923:     """add dgilib"""
1.21      casties   924:     newObj=zogiLib(id,title,digilibBaseUrl, localFileBase, version, basePath)
1.1       dwinter   925:     self.Destination()._setObject(id,newObj)
                    926:     if RESPONSE is not None:
                    927:         RESPONSE.redirect('manage_main')

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