"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/attachable.py $
$Id: attachable.py 23118 2003-11-10 22:13:29Z dbinger $

Provides the Attachable mixin class that allows objects to to have
file attachments.
"""

from mx import DateTime
from dulcinea.typeutils import typecheck
from dulcinea.stored_file import StoredFile

class Attachment:
    """
    Stores properties of an attachment relationship between an Attachable
    object and a StoredFile.

    Instance attributes:
      file : StoredFile
        the file attached
      owner : User
        the owner of this connection object
      date : DateTime
        date this attachment was created
      filename : string
        the filename to use when downloading the file
      description : string
        a description of the file
    """

    def __init__(self, file, owner):
        typecheck(file, StoredFile)
        self.file = file
        self.owner = owner
        self.date = DateTime.now()
        self.filename = file.get_filename()
        self.description = file.get_description()

    def get_file(self):
        return self.file

    def open(self):
        return self.file.open()

    def get_owner(self):
        return self.owner

    def get_date(self):
        return self.date

    def get_filename(self):
        return self.filename

    def set_filename(self, filename):
        self.filename = filename

    def get_description(self):
        return self.description

    def set_description(self, description):
        self.description = description

    def get_size(self):
        return self.file.get_size()

    def get_mime_type(self):
        return self.file.get_mime_type()

    def has_manage_access(self, user):
        return (user and (user is self.owner or user.is_admin()))


class Attachable:
    """
    Instance attributes:
      attachments : [ Attachment ]
        attachments connected to this object

    """
    def __init__(self, attachments=None):
        if attachments:
            self.attachments = list(attachments)
        else:
            self.clear_attachments()

    def clear_attachments (self):
        self.attachments = []

    def get_attachments (self):
        """() -> [Attachment]
        Retrieve a list of the attachments for this object.
        """
        return self.attachments

    def get_attached_files (self):
        """() -> [StoredFile]
        Retrieve a list of the file objects attached to this object.
        """
        return [a.get_file() for a in self.attachments]

    def attach_file (self, stored_file, user):
        """(stored_file: StoredFile)
        Attach a file to this object.
        """
        typecheck(stored_file, StoredFile)
        files = self.attachments[:]
        files.append(Attachment(stored_file, user))
        self.attachments = files

    def detach_attachment (self, attachment):
        """(attachment: Attachment)
        Detach an attachment from this object.
        """
        typecheck(attachment, Attachment)
        files = self.attachments[:]
        files.remove(attachment)
        self.attachments = files

    def attachment_modified(self, attachment, user):
        """(attachment : Attachment, user : DulcineaUser)

        'attachment' was modified by 'user'.
        Override this method to intercept modification activity for attachments
        """
        pass

    def get_allowed_mime_types(self):
        """() -> [string]

        Override to restrict the types of files that can be attached
        """
        return [mime_type[0] for mime_type in StoredFile.MIME_TYPES]


