Annotation of zogiLib/zogiLib.py, revision 1.23

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.23    ! casties   494:     def createScalerDiv(self):
        !           495:         """generate scaler img and table with navigation arrows"""
        !           496:    self.checkQuery()
        !           497:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt/zogilib_img_div')).__of__(self)
        !           498:         return pt()
        !           499: 
1.18      casties   500:     def createAuxDiv(self):
                    501:         """generate other divs"""
1.20      casties   502:    self.checkQuery()
                    503:    bt = self.REQUEST.SESSION['browserType']
1.18      casties   504:         if bt.isN4:
                    505:             f = 'zpt/zogilib_divsN4.zpt'
                    506:         else:
                    507:             f = 'zpt/zogilib_divs.zpt'
                    508:         pt=PageTemplateFile(os.path.join(package_home(globals()),f)).__of__(self)
                    509:         return pt()
1.3       dwinter   510: 
1.9       dwinter   511: 
1.18      casties   512:     def option_js(self):
                    513:         """option_js"""
                    514:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/option_js')).__of__(self)
                    515:         return pt()
1.1       dwinter   516: 
1.18      casties   517:     def dl_lib_js(self):
                    518:         """javascript"""
                    519:         return sendFile(self, 'js/dl_lib.js', 'text/plain')
                    520: 
                    521:     def js_lib_js(self):
                    522:         """javascript"""
                    523:         return sendFile(self, 'js/js_lib.js', 'text/plain')
1.1       dwinter   524: 
1.18      casties   525:     def optionwindow(self):
                    526:         """showoptions"""
                    527:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/optionwindow.zpt')).__of__(self)
                    528:         return pt()
1.13      casties   529: 
                    530:     def mark1(self):
                    531:         """mark image"""
1.18      casties   532:         return sendFile(self, 'images/mark1.gif', 'image/gif')
1.13      casties   533: 
                    534:     def mark2(self):
                    535:         """mark image"""
1.18      casties   536:         return sendFile(self, 'images/mark2.gif', 'image/gif')
1.13      casties   537: 
                    538:     def mark3(self):
                    539:         """mark image"""
1.18      casties   540:         return sendFile(self, 'images/mark3.gif', 'image/gif')
1.13      casties   541: 
                    542:     def mark4(self):
                    543:         """mark image"""
1.18      casties   544:         return sendFile(self, 'images/mark4.gif', 'image/gif')
1.13      casties   545: 
                    546:     def mark5(self):
                    547:         """mark image"""
1.18      casties   548:         return sendFile(self, 'images/mark5.gif', 'image/gif')
1.13      casties   549: 
                    550:     def mark6(self):
                    551:         """mark image"""
1.18      casties   552:         return sendFile(self, 'images/mark6.gif', 'image/gif')
1.13      casties   553: 
                    554:     def mark7(self):
                    555:         """mark image"""
1.18      casties   556:         return sendFile(self, 'images/mark7.gif', 'image/gif')
1.13      casties   557: 
                    558:     def mark8(self):
                    559:         """mark image"""
1.18      casties   560:         return sendFile(self, 'images/mark8.gif', 'image/gif')
1.13      casties   561: 
                    562:     def corner1(self):
                    563:         """mark image"""
1.18      casties   564:         return sendFile(self, 'images/olinks.gif', 'image/gif')
1.13      casties   565: 
                    566:     def corner2(self):
                    567:         """mark image"""
1.18      casties   568:         return sendFile(self, 'images/orechts.gif', 'image/gif')
1.13      casties   569: 
                    570:     def corner3(self):
                    571:         """mark image"""
1.18      casties   572:         return sendFile(self, 'images/ulinks.gif', 'image/gif')
1.13      casties   573: 
                    574:     def corner4(self):
                    575:         """mark image"""
1.18      casties   576:         return sendFile(self, 'images/urechts.gif', 'image/gif')
1.13      casties   577: 
1.22      casties   578:     def up_img(self):
                    579:         """mark image"""
                    580:         return sendFile(self, 'images/up.gif', 'image/gif')
                    581: 
                    582:     def down_img(self):
                    583:         """mark image"""
                    584:         return sendFile(self, 'images/down.gif', 'image/gif')
                    585: 
                    586:     def left_img(self):
                    587:         """mark image"""
                    588:         return sendFile(self, 'images/left.gif', 'image/gif')
                    589: 
                    590:     def right_img(self):
                    591:         """mark image"""
                    592:         return sendFile(self, 'images/right.gif', 'image/gif')
1.13      casties   593: 
                    594: 
1.1       dwinter   595:             
                    596:     def index_html(self):
                    597:         """main action"""
1.18      casties   598:         tp = "zogiLibMainTemplate"
                    599:         if hasattr(self, tp):
                    600:        pt = getattr(self, tp)
                    601:         else:
                    602:             pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/zogiLibMain_%s'%self.layout)).__of__(self)
                    603:         return pt()
                    604: 
1.1       dwinter   605: 
                    606: 
1.21      casties   607:     def storeQuery(self, more = None):
1.1       dwinter   608:         """storeQuery in session"""
1.18      casties   609:         dlParams = {}
1.1       dwinter   610:         for fm in self.REQUEST.form.keys():
1.18      casties   611:             dlParams[fm] = self.REQUEST.form[fm]
1.21      casties   612:         # look for more
                    613:         if more:
                    614:             for fm in more.split('&'):
                    615:                 try:
                    616:                     pv = fm.split('=')
                    617:                     dlParams[pv[0]] = pv[1]
                    618:                 except:
                    619:                     print "ouch!"
                    620:         # parse digilib mode parameter
1.18      casties   621:         if 'mo' in dlParams:
                    622:             if len(dlParams['mo']) > 0:
                    623:                 modes=dlParams['mo'].split(',')
                    624:         else:
                    625:             modes=[]
                    626: 
                    627:         self.REQUEST.SESSION['query'] = dlParams
                    628:         self.REQUEST.SESSION['dlModes'] = modes
                    629:         self.REQUEST.SESSION['dlInfo'] = self.getDLInfo()
                    630:         self.REQUEST.SESSION['browserType'] = BrowserCheck(self)
1.1       dwinter   631: 
1.20      casties   632:     def checkQuery(self):
                    633:    """check if the query has been stored"""
1.21      casties   634:    if not (self.REQUEST.SESSION):
1.20      casties   635:        print "ZOGILIB: have to store query!!"
                    636:        storeQuery(self)
1.23    ! casties   637: 
        !           638:     def zogilibPath(self):
        !           639:         """returns an URL to the zogiLib instance"""
        !           640:         url = self.REQUEST['URL1']
        !           641:         url += self.basePath
        !           642:         # should end with "/"
        !           643:         if len(url) > 0 and url[-1] != '/':
        !           644:             url += '/'
        !           645:         return url
1.3       dwinter   646:         
1.22      casties   647:     def getDLParam(self, param):
1.18      casties   648:         """returns parameter"""
1.3       dwinter   649:         try:
                    650:             return self.REQUEST.SESSION['query'][param]
                    651:         except:
                    652:             return None
                    653: 
1.18      casties   654:     def setDLParam(self, param, value):
                    655:         """sets parameter"""
                    656:         self.REQUEST.SESSION['query'][param] = value
                    657:         return
                    658: 
                    659:     def getAllDLParams(self):
                    660:         """parameter string for digilib"""
                    661:         dlParams = self.REQUEST.SESSION['query']
                    662:         # save modes
                    663:         modes = self.REQUEST.SESSION['dlModes']
                    664:         dlParams['mo'] = string.join(modes, ',')
                    665:         # assemble query string
                    666:         ret = ""
                    667:         for param in dlParams.keys():
                    668:             val = str(dlParams[param])
                    669:             if val != "":
                    670:                 ret += param + "=" + val + "&"
                    671:         # omit trailing "&"
                    672:         return ret.rstrip('&')
                    673: 
                    674:         
                    675:     def setDLParams(self,pn=None,ws=None,rot=None,brgt=None,cont=None):
                    676:         """setze Parameter"""
                    677: 
1.23    ! casties   678:         self.setDLParam('brgt', brgt)
        !           679:         self.setDLParam('cont', cont)
        !           680:         self.setDLParam('ws', ws)
        !           681:         self.setDLParam('rot', rot)
1.18      casties   682: 
                    683:         if pn:
1.21      casties   684:             # unmark
                    685:             self.setDLParam('mk', None)
1.18      casties   686:             self.setDLParam('pn', pn)
                    687:             
                    688:         return self.display()
                    689: 
                    690: 
                    691:     def display(self):
                    692:         """(re)display page"""
                    693:         params = self.getAllDLParams()
1.21      casties   694:         if self.basePath:
                    695:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL2']+'?'+params)
                    696:         else:
                    697:             self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'?'+params)
1.18      casties   698: 
                    699:     def getPT(self):
                    700:         """pagenums"""
                    701:         di = self.REQUEST.SESSION['dlInfo']
                    702:         if di:
                    703:             return int(di['pt'])
                    704:         else:
                    705:             return 1
                    706:     
                    707:     def getPN(self):
                    708:         """Pagenum"""
                    709:         pn = self.getDLParam('pn')
1.3       dwinter   710:         if pn:
1.18      casties   711:             return int(pn)
1.3       dwinter   712:         else:
                    713:             return 1
                    714: 
1.18      casties   715:     def getBiggerWS(self):
1.3       dwinter   716:         """ws+1"""
1.23    ! casties   717:         ws = self.getDLParam('ws')
        !           718:         print "ws: ", ws
1.3       dwinter   719:         if ws:
1.23    ! casties   720:             print "WS!"
1.3       dwinter   721:             return int(ws)+1
                    722:         else:
                    723:             return 2
                    724:         
1.18      casties   725:     def getSmallerWS(self):
                    726:         """ws-1"""
                    727:         ws=self.getDLParam('ws')
1.3       dwinter   728:         if ws:
1.23    ! casties   729:             return max(int(ws)-1, 1)
1.3       dwinter   730:         else:
                    731:             return 1
1.1       dwinter   732: 
1.18      casties   733:     def hasMode(self, mode):
                    734:         """returns if mode is in the diglib mo parameter"""
                    735:         return (mode in self.REQUEST.SESSION['dlModes'])
                    736: 
                    737:     def hasNextPage(self):
                    738:         """returns if there is a next page"""
                    739:         pn = self.getPN()
                    740:         pt = self.getPT()
                    741:         return (pn < pt)
                    742:    
                    743:     def hasPrevPage(self):
                    744:         """returns if there is a previous page"""
                    745:         pn = self.getPN()
                    746:         return (pn > 1)
1.1       dwinter   747: 
1.22      casties   748:     def canMoveLeft(self):
                    749:         """returns if its possible to move left"""
                    750:         wx = float(self.getDLParam('wx') or 0)
                    751:         return (wx > 0)
                    752: 
                    753:     def canMoveRight(self):
                    754:         """returns if its possible to move right"""
                    755:         wx = float(self.getDLParam('wx') or 0)
                    756:         ww = float(self.getDLParam('ww') or 1)
                    757:         return (wx + ww < 1)
                    758: 
                    759:     def canMoveUp(self):
                    760:         """returns if its possible to move up"""
                    761:         wy = float(self.getDLParam('wy') or 0)
                    762:         return (wy > 0)
                    763: 
                    764:     def canMoveDown(self):
                    765:         """returns if its possible to move down"""
                    766:         wy = float(self.getDLParam('wy') or 0)
                    767:         wh = float(self.getDLParam('wh') or 1)
                    768:         return (wy + wh < 1)
                    769: 
1.1       dwinter   770:         
1.18      casties   771:     def dl_HMirror(self):
                    772:         """mirror action"""
                    773:         modes = self.REQUEST.SESSION['dlModes']
                    774:         if 'hmir' in modes:
                    775:             modes.remove('hmir')
                    776:         else:
                    777:             modes.append('hmir')
1.1       dwinter   778: 
1.18      casties   779:         return self.display()
                    780:        
                    781:     def dl_VMirror(self):
                    782:         """mirror action"""
                    783:         modes = self.REQUEST.SESSION['dlModes']
                    784:         if 'vmir' in modes:
                    785:             modes.remove('vmir')
                    786:         else:
                    787:             modes.append('vmir')
1.1       dwinter   788: 
1.18      casties   789:         return self.display()
1.1       dwinter   790: 
1.22      casties   791:     def dl_Zoom(self, z):
                    792:         """general zoom action"""
                    793:         ww1 = float(self.getDLParam('ww') or 1)
                    794:         wh1 = float(self.getDLParam('wh') or 1)
                    795:         wx = float(self.getDLParam('wx') or 0)
                    796:         wy = float(self.getDLParam('wy') or 0)
                    797:         ww2 = ww1 * z
                    798:         wh2 = wh1 * z
                    799:         wx += (ww1 - ww2) / 2
                    800:         wy += (wh1 - wh2) / 2
                    801:         ww2 = max(min(ww2, 1), 0)
                    802:         wh2 = max(min(wh2, 1), 0)
                    803:         wx = max(min(wx, 1), 0)
                    804:         wy = max(min(wy, 1), 0)
                    805:         self.setDLParam('ww', ww2)
                    806:         self.setDLParam('wh', wh2)
                    807:         self.setDLParam('wx', wx)
                    808:         self.setDLParam('wy', wy)
                    809:         return self.display()
                    810:         
                    811:     def dl_ZoomIn(self):
                    812:         """zoom in action"""
                    813:         z = 0.7071
                    814:         return self.dl_Zoom(z)
                    815: 
                    816:     def dl_ZoomOut(self):
                    817:         """zoom out action"""
                    818:         z = 1.4142
                    819:         return self.dl_Zoom(z)
                    820: 
                    821:     def dl_Move(self, dx, dy):
                    822:         """general move action"""
                    823:         ww = float(self.getDLParam('ww') or 1)
                    824:         wh = float(self.getDLParam('wh') or 1)
                    825:         wx = float(self.getDLParam('wx') or 0)
                    826:         wy = float(self.getDLParam('wy') or 0)
                    827:         wx += dx * 0.5 * ww
                    828:         wy += dy * 0.5 * wh
                    829:         wx = max(min(wx, 1), 0)
                    830:         wy = max(min(wy, 1), 0)
                    831:         self.setDLParam('wx', wx)
                    832:         self.setDLParam('wy', wy)
                    833:         return self.display()
                    834:         
                    835:     def dl_MoveLeft(self):
                    836:         """move left action"""
                    837:         return self.dl_Move(-1, 0)
                    838:     
                    839:     def dl_MoveRight(self):
                    840:         """move left action"""
                    841:         return self.dl_Move(1, 0)
                    842:     
                    843:     def dl_MoveUp(self):
                    844:         """move left action"""
                    845:         return self.dl_Move(0, -1)
                    846:     
                    847:     def dl_MoveDown(self):
                    848:         """move left action"""
                    849:         return self.dl_Move(0, 1)
                    850:     
1.18      casties   851:     def dl_WholePage(self):
                    852:         """zoom out action"""
                    853:         self.setDLParam('ww', 1)
                    854:         self.setDLParam('wh', 1)
                    855:         self.setDLParam('wx', 0)
                    856:         self.setDLParam('wy', 0)
                    857:         return self.display()
                    858:         
                    859:     def dl_PrevPage(self):
                    860:         """next page action"""
                    861:         pn = self.getPN() - 1
                    862:         if pn < 1:
                    863:             pn = 1
                    864:         self.setDLParam('pn', pn)
                    865:         # unmark
                    866:         self.setDLParam('mk', None)
                    867:         return self.display()
                    868:         
                    869:     def dl_NextPage(self):
                    870:         """next page action"""
                    871:         pn = self.getPN() + 1
                    872:         pt = self.getPT()
                    873:         if pn > pt:
                    874:             pn = pt
                    875:         self.setDLParam('pn', pn)
                    876:         # unmark
                    877:         self.setDLParam('mk', None)
                    878:         return self.display()
                    879: 
                    880:     def dl_FirstPage(self):
                    881:         """first page action"""
                    882:         self.setDLParam('pn', 1)
                    883:         # unmark
                    884:         self.setDLParam('mk', None)
                    885:         return self.display()
                    886:     
                    887:     def dl_LastPage(self):
                    888:         """last page action"""
                    889:         self.setDLParam('pn', self.getPT())
                    890:         # unmark
                    891:         self.setDLParam('mk', None)
                    892:         return self.display()
                    893: 
                    894:     def dl_Unmark(self):
                    895:         """action to remove last mark"""
                    896:         mk = self.getDLParam('mk')
                    897:         if mk:
                    898:             marks = mk.split(',')
                    899:             marks.pop()
                    900:             mk = string.join(marks, ',')
                    901:             self.setDLParam('mk', mk)
                    902:         return self.display()
1.1       dwinter   903: 
                    904: 
                    905: 
1.18      casties   906:     def changeZogiLibForm(self):
                    907:         """Main configuration"""
                    908:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/changeZogiLibForm.zpt')).__of__(self)
                    909:         return pt()
1.1       dwinter   910:     
1.21      casties   911:     def changeZogiLib(self,title,digilibBaseUrl, localFileBase, version, basePath, RESPONSE=None):
1.18      casties   912:         """change it"""
                    913:         self.title=title
                    914:         self.digilibBaseUrl=digilibBaseUrl
                    915:         self.localFileBase=localFileBase
1.21      casties   916:         self.basePath = basePath
1.18      casties   917:         self.layout=version
1.3       dwinter   918: 
1.18      casties   919:         if RESPONSE is not None:
                    920:             RESPONSE.redirect('manage_main')
1.8       dwinter   921: 
                    922:           
1.1       dwinter   923: def manage_addZogiLibForm(self):
                    924:     """interface for adding zogilib"""
1.18      casties   925:     pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/addZogiLibForm')).__of__(self)
1.1       dwinter   926:     return pt()
                    927: 
1.21      casties   928: def manage_addZogiLib(self,id,title,digilibBaseUrl, localFileBase,version="book",basePath="",RESPONSE=None):
1.1       dwinter   929:     """add dgilib"""
1.21      casties   930:     newObj=zogiLib(id,title,digilibBaseUrl, localFileBase, version, basePath)
1.1       dwinter   931:     self.Destination()._setObject(id,newObj)
                    932:     if RESPONSE is not None:
                    933:         RESPONSE.redirect('manage_main')

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