# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#    Copyright (c) 1999 Christian Scholz (ruebe@aachen.heimat.de)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import xml.dom.minidom
domimpl = xml.dom.minidom.getDOMImplementation()
import urlparse
import urllib
from DAV import utils
from DAV.propfind import PROPFIND
import tools


super_mk_prop_response = PROPFIND.mk_prop_response
def mk_prop_response(self, uri, good_props, bad_props, doc):        
    """ make a new <prop> result element 

    We differ between the good props and the bad ones for
    each generating an extra <propstat>-Node (for each error
    one, that means).
    
    """      
    re=doc.createElement("D:response")
    # append namespaces to response
    nsnum=0
    for nsname in self.namespaces:
        re.setAttribute("xmlns:ns"+str(nsnum),nsname)
        nsnum=nsnum+1
    
    # write href information
    uparts=urlparse.urlparse(uri)
    fileloc=uparts[2]
    if isinstance(fileloc, unicode):
        fileloc = fileloc.encode('utf-8')
    href=doc.createElement("D:href")
    davpath = self._dataclass.parent.get_davpath()
    hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
    huri=doc.createTextNode(hurl)
    href.appendChild(huri)
    re.appendChild(href)

    # write good properties
    ps=doc.createElement("D:propstat")
    if good_props:
        re.appendChild(ps)

    gp=doc.createElement("D:prop")
    for ns in good_props.keys():
        ns_prefix="ns"+str(self.namespaces.index(ns))+":"
        for p,v in good_props[ns].items():
            if not v:
                continue
            pe=doc.createElement(ns_prefix+str(p))
            if hasattr(v, '__class__') and v.__class__.__name__ == 'Element':
                pe.appendChild(v)
            else:
                if ns == 'DAV:' and p=="resourcetype":
                    if v == 1:
                        ve=doc.createElement("D:collection")
                        pe.appendChild(ve)
                elif isinstance(v,tuple) and v[1] == ns:
                    ve=doc.createElement(ns_prefix+v[0])
                    pe.appendChild(ve)
                else:
                    ve=doc.createTextNode(tools.ustr(v))
                    pe.appendChild(ve)

            gp.appendChild(pe)
    
    ps.appendChild(gp)
    s=doc.createElement("D:status")
    t=doc.createTextNode("HTTP/1.1 200 OK")
    s.appendChild(t)
    ps.appendChild(s)
    re.appendChild(ps)

    # now write the errors!
    if len(bad_props.items()):

        # write a propstat for each error code
        for ecode in bad_props.keys():
            ps=doc.createElement("D:propstat")
            re.appendChild(ps)
            bp=doc.createElement("D:prop")
            ps.appendChild(bp)

            for ns in bad_props[ecode].keys():
                ns_prefix="ns"+str(self.namespaces.index(ns))+":"
            
            for p in bad_props[ecode][ns]:
                pe=doc.createElement(ns_prefix+str(p))
                bp.appendChild(pe)
            
            s=doc.createElement("D:status")
            t=doc.createTextNode(utils.gen_estring(ecode))
            s.appendChild(t)
            ps.appendChild(s)
            re.appendChild(ps)

    # return the new response element
    return re
    

def mk_propname_response(self,uri,propnames,doc):
    """ make a new <prop> result element for a PROPNAME request 

    This will simply format the propnames list.
    propnames should have the format {NS1 : [prop1, prop2, ...], NS2: ...}

    """
    re=doc.createElement("D:response")

    # write href information
    uparts=urlparse.urlparse(uri)
    fileloc=uparts[2]
    if isinstance(fileloc, unicode):
        fileloc = fileloc.encode('utf-8')
    href=doc.createElement("D:href")
    davpath = self._dataclass.parent.get_davpath()
    hurl = '%s://%s%s%s' % (uparts[0], uparts[1], davpath, urllib.quote(fileloc))
    huri=doc.createTextNode(hurl)
    href.appendChild(huri)
    re.appendChild(href)

    ps=doc.createElement("D:propstat")
    nsnum=0

    for ns,plist in propnames.items():
        # write prop element
        pr=doc.createElement("D:prop")
        nsp="ns"+str(nsnum)
        pr.setAttribute("xmlns:"+nsp,ns)
        nsnum=nsnum+1

        # write propertynames
        for p in plist:
            pe=doc.createElement(nsp+":"+p)
            pr.appendChild(pe)

        ps.appendChild(pr)

    re.appendChild(ps)

    return re

PROPFIND.mk_prop_response = mk_prop_response
PROPFIND.mk_propname_response = mk_propname_response


@@ -1621,16 +1621,112 @@
 nsnum=0%0A
+    namespaces = self.namespaces%0A    if 'DAV:' in namespaces:%0A        namespaces.remove('DAV:')%0A
     for 
@@ -1735,21 +1735,16 @@
 name in 
-self.
 namespac
@@ -2423,32 +2423,104 @@
 d_props.keys():%0A
+        if ns == 'DAV:':%0A            ns_prefix = 'D:'%0A        else:%0A    
         ns_prefi
@@ -2522,37 +2522,32 @@
 prefix=%22ns%22+str(
-self.
 namespaces.index
@@ -3833,32 +3833,126 @@
 %5Becode%5D.keys():%0A
+                if ns == 'DAV:':%0A                    ns_prefix='D:'%0A                else:%0A    
                 
@@ -3993,32 +3993,32 @@
 .index(ns))+%22:%22%0A
-
             %0A   
@@ -5272,32 +5272,96 @@
 ement(%22D:prop%22)%0A
+        if ns == 'DAV':%0A            nsp = 'D'%0A        else:%0A    
         nsp=%22ns%22
@@ -5380,18 +5380,22 @@
         
+ 
-pr
+   ps
 .setAttr
@@ -5417,16 +5417,20 @@
 nsp,ns)%0A
+    
         

Doc Webdav: compact xml namespaces.